상위 150개 인터뷰쉬움

썩어가는 오렌지

'썩어가는 오렌지' 문제에 대한 자세한 가이드 및 Python 구현.

문제 설명

쉬움

각 셀이 세 가지 값 중 하나를 가질 수 있는 m x n 그리드가 제공됩니다.

- 0은 빈 셀을 나타냅니다.

- 신선한 오렌지를 나타내는 1, 또는

- 2는 썩은 오렌지를 나타냅니다.

1분마다 썩은 오렌지와 4방향으로 인접한 신선한 오렌지는 모두 썩습니다.

어떤 셀에도 신선한 오렌지가 없을 때까지 경과해야 하는 최소 시간(분)을 반환합니다. 이것이 불가능하다면 -1을 반환합니다.

orangesRotting(grid: List[List[int]]) -> int 함수를 작성하세요.

제약
  • m == len(grid)
  • n == len(grid[i])
  • 1 <= m, n <= 10
  • grid[i][j] is 0, 1, or 2

Example 1
Input
grid = [[2,1,1],[1,1,0],[0,1,1]]
Output
4
Explanation

Minute 0: rotten at (0,0). Fresh at (0,1), (0,2), (1,0), (1,1), (2,1), (2,2). Minute 1: fresh at (0,1) and (1,0) rot. Minute 2: fresh at (0,2) and (1,1) rot. Minute 3: fresh at (2,1) rot. Minute 4: fresh at (2,2) rot.

Example 2
Input
grid = [[2,1,1],[0,1,1],[1,0,1]]
Output
-1
Explanation

The orange in the bottom-left corner (row 2, column 0) is never adjacent to a rotten orange, so it stays fresh.

Need a Hint?
세트나 힙과 같은 그래프 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.