Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3587ce1

Browse files
Add solution for Where Will the Ball Fall
1 parent 275b637 commit 3587ce1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Algorithm exercises from LeetCode implemented in Java (v11) and JavaScript.
2020
- Cells with Odd Values in a Matrix | [Problem](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix) | [Java Solution](src/javacode/solutions/OddCellsInMatrix.java)
2121
- Island Perimeter | [Problem](https://leetcode.com/problems/island-perimeter) | [Java Solution](src/javacode/solutions/IslandPerimeter.java)
2222
- Valid Sudoku | [Problem](https://leetcode.com/problems/valid-sudoku) | [Java Solution](src/javacode/solutions/ValidSudoku.java)
23+
- Where Will the Ball Fall | [Problem](https://leetcode.com/problems/where-will-the-ball-fall) | [Java Solution](src/javacode/solutions/WhereWillTheBallFall.java)
2324

2425
### HashTable
2526
- Two Sum | [Problem](https://leetcode.com/problems/two-sum) | [Java Solution](src/javacode/solutions/TwoSum.java) | [JS Solution](src/javascript/solutions/twoSum.js)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package javacode.solutions;
2+
3+
import java.util.Arrays;
4+
5+
// [Problem] https://leetcode.com/problems/where-will-the-ball-fall
6+
class WhereWillTheBallFall {
7+
// Matrix
8+
// O(m * n) time, O(n) space
9+
// where m = row size, n = column size
10+
public int[] findBall(int[][] grid) {
11+
int rowSize = grid.length, colSize = grid[0].length;
12+
int[] ballPositions = new int[colSize];
13+
for (int ball = 0; ball < colSize; ball++) {
14+
int row = 0, col = ball;
15+
while (row < rowSize) {
16+
int direction = grid[row][col];
17+
int nextCol = col + direction;
18+
if (nextCol < 0 || nextCol >= colSize || grid[row][nextCol] != direction) {
19+
ballPositions[ball] = -1;
20+
break;
21+
}
22+
row++;
23+
col = nextCol;
24+
}
25+
if (row == rowSize) {
26+
ballPositions[ball] = col;
27+
}
28+
}
29+
return ballPositions;
30+
}
31+
32+
// Test
33+
public static void main(String[] args) {
34+
WhereWillTheBallFall solution = new WhereWillTheBallFall();
35+
36+
int[][] input = {
37+
{1, 1, 1, -1, -1},
38+
{1, 1, 1, -1, -1},
39+
{-1, -1, -1, 1, 1},
40+
{1, 1, 1, 1, -1},
41+
{-1, -1, -1, -1, -1}
42+
};
43+
int[] expectedOutput = {1, -1, -1, -1, -1};
44+
int[] actualOutput = solution.findBall(input);
45+
46+
System.out.println("Test passed? " + Arrays.equals(expectedOutput, actualOutput));
47+
}
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /