This is a problem that I am trying to solve on my own in order to improve recursion a bit (rather than homework). I believe that I have found a solution, but I'm not sure about the complexity of the time (I know that DP will give me better results).
Find all the ways in which you can climb the n step ladder if you can take k steps at a time, so k <= n
For example, if the step size is [1,2,3], and the stairwell is 10, I could perform 10 steps of size 1 [1,1,1,1,1,1,1,1, 1,1,1] = 10 or I could do 3 steps of size 3 and 1 step of size 1 [3,3,3,1] = 10
Here is my solution:
static List<List<Integer>> problem1Ans = new ArrayList<List<Integer>>(); public static void problem1(int numSteps){ int [] steps = {1,2,3}; problem1_rec(new ArrayList<Integer>(), numSteps, steps); } public static void problem1_rec(List<Integer> sequence, int numSteps, int [] steps){ if(problem1_sum_seq(sequence) > numSteps){ return; } if(problem1_sum_seq(sequence) == numSteps){ problem1Ans.add(new ArrayList<Integer>(sequence)); return; } for(int stepSize : steps){ sequence.add(stepSize); problem1_rec(sequence, numSteps, steps); sequence.remove(sequence.size()-1); } } public static int problem1_sum_seq(List<Integer> sequence){ int sum = 0; for(int i : sequence){ sum += i; } return sum; } public static void main(String [] args){ problem1(10); System.out.println(problem1Ans.size()); }
I assume that this is the runtime k ^ n, where k is the number of step sizes and n is the number of steps (in this case 3 and 10).
I came to this answer because each step size has a loop that causes k number of steps. However, the depth of this is not the same for all step sizes. For example, the sequence [1,1,1,1,1,1,1,1,1,1,1,1,1] has more recursive calls than [3,3,3,1], so it makes me doubt my answer .
What is the lead time? Is k ^ n correct?