-
Notifications
You must be signed in to change notification settings - Fork 0
994. Rotting Oranges #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| def get_initial_rotten_oranges_num_and_coordinates(self, grid: List[List[int]]) -> Tuple[int, Set[Tuple[int, int]]]: | ||
| num_rotten = 0 | ||
| coordinates_rotten = set() | ||
| for y in range(len(grid)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
len(grid) と len(grid[0]) が複数回登場するため、それぞれ num_rows, num_columns に代入してあげると読みやすくなると思いました。
| return num_oranges | ||
|
|
||
| def is_fresh(self, x: int, y: int, grid: List[List[int]], coordinates_rotten: Set[Tuple[int, int]]) -> bool: | ||
| if not 0 <= x < len(grid[0]) or not 0 <= y < len(grid): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not (0 <= x < len(grid[0]) and 0 <= y < len(grid)): のほうがややシンプルだと思いました。
| FRESH = 1 | ||
| ROTTEN = 2 | ||
|
|
||
| def get_initial_rotten_oranges_num_and_coordinates(self, grid: List[List[int]]) -> Tuple[int, Set[Tuple[int, int]]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
protected method のため、先頭に _ を付けてあげるとよいと思いました。
| if not previously_rotten: | ||
| return -1 | ||
| for x, y in previously_rotten: | ||
| if self.is_fresh(x + 1, y, grid, coordinates_rotten): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deltas = [(0, 1), (0, -1), (1, 0), (-1, 0)]
...
for delta in deltas:
if (self.is_fresh(x + delta[0], y + delta[1], grid, coordinates_rotten)):といった感じにすると、コードの重複が減ると思いました。
| if any(cell == self.FRESH for row in rotting_grid for cell in row): | ||
| return -1 | ||
| return minites_plus_one - 1 | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
時間を進めるタイミングが難しいですね。素直に問題文の通りに表現すると
すべてのオレンジが腐っているかを確認して、その場合は return
時間を進めて、隣のオレンジを腐らせる。
状況が変わっていなかったら return -1
という無限ループでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。確かにこれが自然ですね
# 略
def orangesRotting(self, grid: List[List[Orange]]) -> int:
minutes = 0
rotten_oranges_so_far = set([(x, y) for y in range(len(grid)) for x in range(len(grid[0])) if grid[y][x] == Orange.ROTTEN])
num_oranges_all = sum(grid[y][x] != Orange.EMPTY for y in range(len(grid)) for x in range(len(grid[0])))
while True:
if len(rotten_oranges_so_far) == num_oranges_all:
return minutes
new_rotten_oranges = self._rot_next_oranges(grid, rotten_oranges_so_far)
minutes += 1
if len(new_rotten_oranges) == 0:
return -1
rotten_oranges_so_far |= new_rotten_orangesこれもアリかなと思ってました
増やせなかったときに、なのに全部腐らせることができなかったら-1
# 略
def orangesRotting(self, grid: List[List[Orange]]) -> int:
minutes = 0
rotten_oranges_so_far = set([(x, y) for y in range(len(grid)) for x in range(len(grid[0])) if grid[y][x] == Orange.ROTTEN])
num_oranges_all = sum(grid[y][x] != Orange.EMPTY for y in range(len(grid)) for x in range(len(grid[0])))
while True:
new_rotten_oranges = self._rot_next_oranges(grid, rotten_oranges_so_far)
if len(new_rotten_oranges) == 0:
if len(rotten_oranges_so_far) < num_oranges_all:
return -1
return minutes
rotten_oranges_so_far |= new_rotten_oranges
minutes += 1
hayashi-ay
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
すごく気になるところなどはなかったです。
| - 意図は明確になるように感じるけど、長くなりがち? | ||
| - minites_plus_oneは流石にわかりにくいかも。 | ||
| - while oranges_rottenのすぐ後でminites_plus_one += 1したいなあと思ってたらこうなってしまった。 | ||
| - あとで気づいたが、xとyが逆でした |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的にはx,yで命名すると結構混乱する気がするんですよね。row, colとかのほうが個人的にはわかりやすいです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。rowとcolも自分は結構混乱するんですよね、、、(が、これは自分が覚えた方が良い気がしてきました)
| minites_plus_one = 0 | ||
| if all(cell == self.EMPTY for row in grid for cell in row): | ||
| return 0 | ||
| oranges_rotten = [(x, y) for y in range(len(grid[0])) for x in range(len(grid)) if grid[x][y] == self.ROTTEN] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
英語の語順的にはrotten_orangesになるかなと思います
URL: https://leetcode.com/problems/rotting-oranges/description/
You are given an m x n grid where each cell can have one of three values:
0 representing an empty cell,
1 representing a fresh orange, or
2 representing a rotten orange.
Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.
Example 1:
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Example 2: