-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Solution.java
35 lines (31 loc) · 1020 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// github.com/RodneyShag
import java.util.Scanner;
import java.util.Arrays;
// Time Complexity: O(N + T)
// Space Complexity: O(N + T)
public class Solution {
public static void main(String[] args) {
/* Save input */
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
int [] testcase = new int[T];
for (int i = 0; i < T; i++) {
testcase[i] = scan.nextInt();
}
/* Calculate answers for all T testcases */
int maxCycles = Arrays.stream(testcase).max().getAsInt();
int [] cache = new int[maxCycles + 1]; // cache is crucial to increase runtime
cache[0] = 1;
for (int i = 1; i <= maxCycles; i++) {
if (i % 2 == 1) {
cache[i] = cache[i-1] * 2;
} else {
cache[i] = cache[i-1] + 1;
}
}
/* Print answers */
for (int i = 0; i < T; i++) {
System.out.println(cache[testcase[i]]);
}
}
}