-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleQ1.java
More file actions
66 lines (57 loc) · 1.88 KB
/
GoogleQ1.java
File metadata and controls
66 lines (57 loc) · 1.88 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// You start at index 0 in an array with length 'h'. At each step, you can move to the left, move to the right, or
// stay in the same place(Note! Stay in the same place also takes one step).
// How many possible ways are you still at index 0 after you have walked 'n' step?
//
// Example: n = 3
// 1. right->left->stay
// 2. right->stay->left
// 3. stay->right->left
// 4. stay->stay->stay
//
// Can anyone solve it in n^2
import java.util.Stack;
public class GoogleQ1 {
private static int solutions;
private static void printStack(Stack<Integer> stack) {
int i = 0;
for (Integer val : stack) {
System.out.print(val);
if (i < stack.size() - 1) {
System.out.print(" -> ");
} else {
System.out.println("");
}
i++;
}
}
private static void findWays(int len, Stack<Integer> stack, int limit) {
int index = stack.peek();
if (stack.size() == limit + 1) {
if (index == 0) {
solutions++;
printStack(stack);
}
return;
}
for (int i = -1; i <= 1; i++) {
if (index + i < 0) {
continue;
} else if (index + i > len - 1) {
continue;
}
Stack<Integer> clone = (Stack<Integer>) stack.clone();
clone.add(index + i);
findWays(len, clone, limit);
}
}
private void solve(int h, int n) {
Stack<Integer> stack = new Stack<>();
stack.add(0);
findWays(h, stack, n);
System.out.println("Having array of " + h + " length and " + n + " steps to take. " +
"You can walk through " + solutions + " ways ending up back on index 0 position");
}
public void run() {
solve(10, 6);
}
}