-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBallSwitchBoard.java
More file actions
50 lines (48 loc) · 973 Bytes
/
BallSwitchBoard.java
File metadata and controls
50 lines (48 loc) · 973 Bytes
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
public class BallSwitchBoard {
public int solution(int[][] A, int K) {
int row = A.length;
int col = A[0].length;
Box[] boxes = null;
for (int i = 0; i < row; i++) {
Box[] nextBoxes = new Box[col];
for (int j = 0; j < col; j++) {
int fromLeft = 0;
int fromUp = 0;
if (j != 0) {
fromLeft = nextBoxes[j - 1].right;
}
if (boxes != null) {
fromUp = boxes[j].down;
} else if (j == 0) {
fromUp = K;
}
int right;
int down;
if (A[i][j] == 0) {
right = fromLeft;
down = fromUp;
} else {
int total = fromLeft + fromUp;
if (A[i][j] == 1) {
down = total / 2;
right = total - down;
} else {
right = total / 2;
down = total - right;
}
}
nextBoxes[j] = new Box(right, down);
}
boxes = nextBoxes;
}
return boxes[col - 1].down;
}
}
class Box {
int right;
int down;
Box(int right, int down) {
this.right = right;
this.down = down;
}
}