鱼C论坛

 找回密码
 立即注册
查看: 1969|回复: 0

[学习笔记] Leetcode 994. Rotting Oranges

[复制链接]
发表于 2020-8-2 03:42:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. In a given grid, each cell can have one of three values:

  2. the value 0 representing an empty cell;
  3. the value 1 representing a fresh orange;
  4. the value 2 representing a rotten orange.
  5. Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.

  6. Return the minimum number of minutes that must elapse until no cell has a fresh orange.  If this is impossible, return -1 instead.



  7. Example 1:
  8. Screenshot from 2020-08-01 15-41-18.png


  9. Input: [[2,1,1],[1,1,0],[0,1,1]]
  10. Output: 4
  11. Example 2:

  12. Input: [[2,1,1],[0,1,1],[1,0,1]]
  13. Output: -1
  14. Explanation:  The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
  15. Example 3:

  16. Input: [[0,2]]
  17. Output: 0
  18. Explanation:  Since there are already no fresh oranges at minute 0, the answer is just 0.


  19. Note:

  20. 1 <= grid.length <= 10
  21. 1 <= grid[0].length <= 10
  22. grid[i][j] is only 0, 1, or 2.
复制代码

  1. class Solution:
  2.     def orangesRotting(self, grid: List[List[int]]) -> int:
  3.         directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
  4.         queue = []
  5.         visited = set()
  6.         m = len(grid)
  7.         n = len(grid[0])
  8.         level = 0
  9.         count = 0
  10.         goal = 0
  11.         c = 0
  12.         
  13.         for i in range(m):
  14.             for j in range(n):
  15.                 if grid[i][j] == 2:
  16.                     queue.append([i, j])
  17.                     visited.add((i, j))
  18.                     count += 1
  19.                     goal += 1
  20.                 if grid[i][j] == 1:
  21.                     count += 1
  22.                     c += 1
  23.         if c == 0:
  24.             return 0
  25.         while queue:
  26.             size = len(queue)
  27.             
  28.             for i in range(size):
  29.                 cur = queue.pop(0)
  30.                 x = cur[0]
  31.                 y = cur[1]
  32.                
  33.                 for direction in directions:
  34.                     new_x = x + direction[0]
  35.                     new_y = y + direction[1]
  36.                     if new_x < 0 or new_x >= m or new_y < 0 or new_y >= n or grid[new_x][new_y] == 0 or (new_x, new_y) in visited:
  37.                         continue
  38.                     
  39.                     if grid[new_x][new_y] == 1 and (new_x, new_y) not in visited:
  40.                         goal += 1
  41.                         grid[new_x][new_y] = 2
  42.                         queue.append([new_x, new_y])
  43.                         visited.add((new_x, new_y))
  44.             level += 1
  45.             if goal == count:
  46.                 return level
  47.         
  48.         for i in range(m):
  49.             for j in range(n):
  50.                 if grid[i][j] == 1:
  51.                     return -1
  52.         return level
  53.             
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 01:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表