Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented May 31, 2025

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:

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)):
Copy link

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):
Copy link

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]]]:
Copy link

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):
Copy link

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
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

時間を進めるタイミングが難しいですね。素直に問題文の通りに表現すると

すべてのオレンジが腐っているかを確認して、その場合は return
時間を進めて、隣のオレンジを腐らせる。
状況が変わっていなかったら return -1

という無限ループでしょうか。

Copy link
Owner Author

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

Copy link

@hayashi-ay hayashi-ay left a 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が逆でした

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的にはx,yで命名すると結構混乱する気がするんですよね。row, colとかのほうが個人的にはわかりやすいです。

Copy link
Owner Author

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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

英語の語順的にはrotten_orangesになるかなと思います

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants