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 25880e9

Browse files
committed
Create 1706-WhereWillTheBallFall.py
1 parent 3d4a357 commit 25880e9

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

‎src/1706-WhereWillTheBallFall.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
"""
3+
CREATED AT: 2022年11月01日
4+
5+
URL: https://leetcode.com/problems/where-will-the-ball-fall/
6+
7+
GITHUB: https://github.com/Jiezhi/myleetcode
8+
9+
FileName: 1706-WhereWillTheBallFall
10+
11+
Difficulty: Medium
12+
13+
Desc:
14+
15+
Tag:
16+
17+
See:
18+
19+
"""
20+
from tool import *
21+
22+
23+
class Solution:
24+
def findBall(self, grid: List[List[int]]) -> List[int]:
25+
"""
26+
Runtime: 246 ms, faster than 82.02%
27+
Memory Usage: 14.6 MB, less than 12.94%
28+
m == grid.length
29+
n == grid[i].length
30+
1 <= m, n <= 100
31+
grid[i][j] is 1 or -1.
32+
"""
33+
m, n = len(grid), len(grid[0])
34+
ret = []
35+
36+
def fall(x, y) -> int:
37+
if x == m and 0 <= y < n:
38+
return y
39+
if not 0 <= x < m or not 0 <= y < n:
40+
return -1
41+
if grid[x][y] == -1:
42+
if y == 0 or grid[x][y - 1] == 1:
43+
return -1
44+
return fall(x + 1, y - 1)
45+
else:
46+
if y == n - 1 or grid[x][y + 1] == -1:
47+
return -1
48+
return fall(x + 1, y + 1)
49+
50+
for i in range(n):
51+
ret.append(fall(0, i))
52+
return ret
53+
54+
55+
def test():
56+
assert Solution().findBall(
57+
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]]) == [1,
58+
-1,
59+
-1,
60+
-1,
61+
-1]
62+
assert Solution().findBall(grid=[[-1]]) == [-1]
63+
64+
65+
if __name__ == '__main__':
66+
test()

0 commit comments

Comments
(0)

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