当前位置:网站首页>Simulation volume leetcode [general] 1706 Where does the ball meet
Simulation volume leetcode [general] 1706 Where does the ball meet
2022-07-07 08:55:00 【Encounter simulation volume】
Summary : Simulation volume Leetcode Summary of questions
1706. Where the ball will go
Use a size of m x n 2-D grid of grid It means a box . Do you have n Ball . The top and bottom of the box are open .
Each cell in the box has a diagonal baffle , Across the two corners of the cell , You can direct the ball to the left or right .
Guide the ball to the right baffle across the upper left and lower right , Use... In the grid 1 Express .
Guide the ball to the left baffle across the upper right and lower left , Use... In the grid -1 Express .
At the top of each box . Every ball can get stuck in the box or fall from the bottom . If the ball happens to be stuck between the two baffles “V” Shape patterns , Or guided by a block to either side of the box , It will get stuck .
Return a size of n Array of answer , among answer[i] On top of the ball is i The subscript of the column falling from the bottom after the column , If the ball gets stuck in the box , Then return to -1 .
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]
explain : The sample is shown in figure :
b0 The ball starts to be in the second position 0 On the column , Finally from the bottom of the box 1 List out .
b1 The ball starts to be in the second position 1 On the column , It's going to get stuck in the second 2、3 Column and the first 1 Between lines “V” Xingli .
b2 The ball starts to be in the second position 2 On the column , It's going to get stuck in the second 2、3 Column and the first 0 Between lines “V” Xingli .
b3 The ball starts to be in the second position 3 On the column , It's going to get stuck in the second 2、3 Column and the first 0 Between lines “V” Xingli .
b4 The ball starts to be in the second position 4 On the column , It's going to get stuck in the second 2、3 Column and the first 1 Between lines “V” Xingli .
Example 2:
Input :grid = [[-1]]
Output :[-1]
explain : The ball is stuck on the left side of the box .
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]
Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 100
grid[i][j] by 1 or -1
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/where-will-the-ball-fall
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def findBall(self, grid: List[List[int]]) -> List[int]:
n = len(grid[0])
res = [-1]*n
for j in range(n):
col = j
for row in grid:
dir = row[col]
col += dir
if col<0 or col==n or row[col]!=dir: break
else:
res[j]=col
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list turn node
return s.getResult(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- Gson转换实体类为json时报declares multiple JSON fields named
- Enterprise manager cannot connect to the database instance
- let const
- 更改当前文件夹及文件夹下文件日期shell脚本
- Gson converts the entity class to JSON times declare multiple JSON fields named
- Calf problem
- Why choose cloud native database
- IP地址的类别
- xray的简单使用
- 【ChaosBlade:根据标签删除POD、Pod 域名访问异常场景、Pod 文件系统 I/O 故障场景】
猜你喜欢
Other 7 features of TCP [sliding window mechanism ▲]
Count sort (diagram)
Greenplum 6.x common statements
Greenplum 6.x version change record common manual
Greenplum6.x重新初始化
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
Digital triangle model acwing 1027 Grid access
路由信息协议——RIP
[MySQL] detailed explanation of trigger content of database advanced
Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and
随机推荐
Introduction to data fragmentation
Gson转换实体类为json时报declares multiple JSON fields named
面试题:高速PCB一般布局、布线原则
A bug using module project in idea
Problems encountered in the use of go micro
模拟卷Leetcode【普通】1705. 吃苹果的最大数目
9c09730c0eea36d495c3ff6efe3708d8
数字三角形模型 AcWing 1027. 方格取数
模拟卷Leetcode【普通】1557. 可以到达所有点的最少点数目
个人力扣题目分类记录
Mock. JS usage details
Opencv converts 16 bit image data to 8 bits and 8 to 16
2022-07-06 Unity核心9——3D动画
Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
idea里使用module项目的一个bug
数字三角形模型 AcWing 275. 传纸条
更改当前文件夹及文件夹下文件日期shell脚本
MySQL主从延迟的解决方案
OpenGL三维图形绘制
Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and