-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhereWillTheBallFall.java
48 lines (43 loc) · 1.5 KB
/
WhereWillTheBallFall.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
36
37
38
39
40
41
42
43
44
45
46
47
48
package solutions;
import java.util.Arrays;
// [Problem] https://leetcode.com/problems/where-will-the-ball-fall
class WhereWillTheBallFall {
// Matrix
// O(m * n) time, O(n) space
// where m = row size, n = column size
public int[] findBall(int[][] grid) {
int rowSize = grid.length, colSize = grid[0].length;
int[] ballPositions = new int[colSize];
for (int ball = 0; ball < colSize; ball++) {
int row = 0, col = ball;
while (row < rowSize) {
int direction = grid[row][col];
int nextCol = col + direction;
if (nextCol < 0 || nextCol >= colSize || grid[row][nextCol] != direction) {
ballPositions[ball] = -1;
break;
}
row++;
col = nextCol;
}
if (row == rowSize) {
ballPositions[ball] = col;
}
}
return ballPositions;
}
// Test
public static void main(String[] args) {
WhereWillTheBallFall solution = new WhereWillTheBallFall();
int[][] input = {
{1, 1, 1, -1, -1},
{1, 1, 1, -1, -1},
{-1, -1, -1, 1, 1},
{1, 1, 1, 1, -1},
{-1, -1, -1, -1, -1}
};
int[] expectedOutput = {1, -1, -1, -1, -1};
int[] actualOutput = solution.findBall(input);
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
}
}