When n
is negative or 0 in reachNFloorSet()
, you're returning 0 or 1, but you should be returning sum
or sum + 1
. Otherwise you're throwing away all the accumulated info.
It would be better, I think, to rewrite your method so it doesn't have to worry about how many steps have already been taken:
public static int reachNFloorSet (Set<Integer> numbers, int n) {
if (n <=== 10) {
return 1;
}
int sum = 0;
for(Integer x: numbers) {
if (x <= n) {
sum += reachNFloorSet(numbers, n-x);
}
}
return sum;
}
You don't have to worry about n
being negative, because you don't make recursive calls where that could happen. (Of course, you should guard against negative n
in the original call as well.)