其他分享
首页 > 其他分享> > [LeetCode] 1706. Where Will the Ball Fall

[LeetCode] 1706. Where Will the Ball Fall

作者:互联网

You have a 2-D grid of size m x n representing a box, and you have n balls. The box is open on the top and bottom sides.

Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.

We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a "V" shaped pattern between two boards or if a board redirects the ball into either wall of the box.

Return an array answer of size n where answer[i] is the column that the ball falls out of at the bottom after dropping the ball from the ith column at the top, or -1 if the ball gets stuck in the box.

Example 1:

Input: grid = [[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]]
Output: [1,-1,-1,-1,-1]
Explanation: This example is shown in the photo.
Ball b0 is dropped at column 0 and falls out of the box at column 1.
Ball b1 is dropped at column 1 and will get stuck in the box between column 2 and 3 and row 1.
Ball b2 is dropped at column 2 and will get stuck on the box between column 2 and 3 and row 0.
Ball b3 is dropped at column 3 and will get stuck on the box between column 2 and 3 and row 0.
Ball b4 is dropped at column 4 and will get stuck on the box between column 2 and 3 and row 1.

Example 2:

Input: grid = [[-1]]
Output: [-1]
Explanation: The ball gets stuck against the left wall.

Example 3:

Input: grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
Output: [0,1,2,3,4,-1] 

Constraints:

球会落何处。

用一个大小为 m x n 的二维网格 grid 表示一个箱子。你有 n 颗球。箱子的顶部和底部都是开着的。

箱子中的每个单元格都有一个对角线挡板,跨过单元格的两个角,可以将球导向左侧或者右侧。

将球导向右侧的挡板跨过左上角和右下角,在网格中用 1 表示。
将球导向左侧的挡板跨过右上角和左下角,在网格中用 -1 表示。
在箱子每一列的顶端各放一颗球。每颗球都可能卡在箱子里或从底部掉出来。如果球恰好卡在两块挡板之间的 "V" 形图案,或者被一块挡导向到箱子的任意一侧边上,就会卡住。

返回一个大小为 n 的数组 answer ,其中 answer[i] 是球放在顶部的第 i 列后从底部掉出来的那一列对应的下标,如果球卡在盒子里,则返回 -1 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/where-will-the-ball-fall
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道模拟题。题意说的很明白了,input 给的是一个 mxn 的矩阵,在矩阵顶部的每个位置上都有一个球,现在让球落下,根据矩阵里每个位置的挡板的情况,输出最后球的位置信息。

注意一开始球的位置是在矩阵外的,所以球落入第一排 matrix[0][y] 也需要判断。因为球只会根据挡板的位置而改变行进方向,而且挡板位置左上 - 右下用 1 表示,右上 - 左下用 -1 表示。这个表示方法正好对应了球下一个位置的方向,因为如果是左上 - 右下,正好是往右下走;如果是右上 - 左下,正好是往左下走。所以这道题我按照题目模拟出来即可。

时间O(mn) - 有 N 个球,每个球最坏情况都会从箱子底部调出来

空间O(n)

Java实现

 1 class Solution {
 2     int rows, cols;
 3 
 4     public int[] findBall(int[][] grid) {
 5         rows = grid.length;
 6         cols = grid[0].length;
 7         int[] res = new int[cols];
 8         for (int i = 0; i < cols; i++) {
 9             res[i] = helper(i, grid);
10         }
11         return res;
12     }
13 
14     private int helper(int n, int[][] grid) {
15         int r = 0;
16         int c = n;
17         while (r < rows) {
18             int nextC = c + grid[r][c];
19             if (nextC < 0 || nextC > cols - 1 || grid[r][c] != grid[r][nextC]) {
20                 return -1;
21             }
22             r++;
23             c = nextC;
24         }
25         return c;
26     }
27 }

 

LeetCode 题目总结

标签:box,Ball,1706,stuck,column,Will,int,ball,grid
来源: https://www.cnblogs.com/cnoodle/p/15949355.html