Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit beb0594

Browse files
committed
Add solution #1706
1 parent 36e774f commit beb0594

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,490 LeetCode solutions in JavaScript
1+
# 1,491 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1315,6 +1315,7 @@
13151315
1703|[Minimum Adjacent Swaps for K Consecutive Ones](./solutions/1703-minimum-adjacent-swaps-for-k-consecutive-ones.js)|Hard|
13161316
1704|[Determine if String Halves Are Alike](./solutions/1704-determine-if-string-halves-are-alike.js)|Easy|
13171317
1705|[Maximum Number of Eaten Apples](./solutions/1705-maximum-number-of-eaten-apples.js)|Medium|
1318+
1706|[Where Will the Ball Fall](./solutions/1706-where-will-the-ball-fall.js)|Medium|
13181319
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13191320
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13201321
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* 1706. Where Will the Ball Fall
3+
* https://leetcode.com/problems/where-will-the-ball-fall/
4+
* Difficulty: Medium
5+
*
6+
* You have a 2-D grid of size m x n representing a box, and you have n balls. The box is open on
7+
* the top and bottom sides.
8+
*
9+
* Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a
10+
* ball to the right or to the left.
11+
* - A board that redirects the ball to the right spans the top-left corner to the bottom-right
12+
* corner and is represented in the grid as 1.
13+
* - A board that redirects the ball to the left spans the top-right corner to the bottom-left
14+
* corner and is represented in the grid as -1.
15+
*
16+
* We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall
17+
* out of the bottom. A ball gets stuck if it hits a "V" shaped pattern between two boards or if a
18+
* board redirects the ball into either wall of the box.
19+
*
20+
* Return an array answer of size n where answer[i] is the column that the ball falls out of at the
21+
* bottom after dropping the ball from the ith column at the top, or -1 if the ball gets stuck in
22+
* the box.
23+
*/
24+
25+
/**
26+
* @param {number[][]} grid
27+
* @return {number[]}
28+
*/
29+
var findBall = function(grid) {
30+
const m = grid.length;
31+
const n = grid[0].length;
32+
const result = new Array(n);
33+
34+
for (let col = 0; col < n; col++) {
35+
let currentCol = col;
36+
let row = 0;
37+
38+
while (row < m) {
39+
const nextCol = currentCol + grid[row][currentCol];
40+
41+
if (nextCol < 0 || nextCol >= n || grid[row][currentCol] !== grid[row][nextCol]) {
42+
currentCol = -1;
43+
break;
44+
}
45+
46+
currentCol = nextCol;
47+
row++;
48+
}
49+
50+
result[col] = currentCol;
51+
}
52+
53+
return result;
54+
};

0 commit comments

Comments
 (0)