Skip to content

Instantly share code, notes, and snippets.

@chrislukkk
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislukkk/2989c58a3dced863cbb0 to your computer and use it in GitHub Desktop.
Save chrislukkk/2989c58a3dced863cbb0 to your computer and use it in GitHub Desktop.
Career cup 9.1
package Chapter9;
public class StepCounter {
public static long countAllWays(int n) {
long wayCount = 0;
return step(n, wayCount);
}
private static long step(int n, long count) {
if(n == 0) return count + 1;
if(n > 0)
count = step(n-1, count);
if(n > 1)
count = step(n-2, count);
if(n > 2)
count = step(n-3, count);
return count;
}
private static long countAllWaysMem(int n) {
long ways[] = new long[n+1];
ways[0] = 1;
for(int i = 1; i <= n; i++) {
ways[i] = ways[i-1];
if(i > 1)
ways[i] += ways[i-2];
if(i > 2)
ways[i] += ways[i-3];
}
return ways[n];
}
public static void main(String[] args) {
System.out.println(countAllWaysMem(100));
System.out.println(countAllWays(30));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment