Skip to content

Commit 83d106d

Browse files
authored
Create DavidsStarcase.java
1 parent 3e4e8f8 commit 83d106d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

DavidsStarcase.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
public class Solution {
10+
11+
//Bottom-up O(n) space and time
12+
static int stepPerms(int n) {
13+
14+
int[] memo = new int[n+1];
15+
16+
for (int i = 0; i <= n; i++) {
17+
if (i == 0) memo[0] = 0;
18+
else if (i == 1) memo[1] = 1;
19+
else if (i == 2) memo[2] = 2;
20+
else if (i == 3) memo[3] = 4;
21+
else memo[i] = memo[i-1] + memo[i-2] + memo[i-3];
22+
}
23+
24+
return memo[n];
25+
26+
}
27+
28+
private static final Scanner scanner = new Scanner(System.in);
29+
30+
public static void main(String[] args) throws IOException {
31+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
32+
33+
int s = scanner.nextInt();
34+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
35+
36+
for (int sItr = 0; sItr < s; sItr++) {
37+
int n = scanner.nextInt();
38+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
39+
40+
int res = stepPerms(n);
41+
42+
bufferedWriter.write(String.valueOf(res));
43+
bufferedWriter.newLine();
44+
}
45+
46+
bufferedWriter.close();
47+
48+
scanner.close();
49+
}
50+
}

0 commit comments

Comments
 (0)