상위 150개 인터뷰쉬움

벽과 문

'벽과 문' 문제에 대한 자세한 가이드 및 Python 구현.

문제 설명

쉬움

다음 세 가지 가능한 값으로 초기화된 m x n 그리드 룸이 제공됩니다.

- -1: 벽이나 장애물.

- 0: 게이트.

- INF(2147483647로 표현) : 빈 방.

각 빈 방을 가장 가까운 게이트까지의 거리로 채웁니다. Gate 도달이 불가능할 경우에는 INF로 채워야 한다.

수정된 방 ​​그리드를 반환하는 wallsAndGates(rooms: List[List[int]]) -> List[List[int]] 함수를 작성하세요.

제약
  • m == len(rooms)
  • n == len(rooms[i])
  • 1 <= m, n <= 250
  • rooms[i][j] is -1, 0, or 2147483647

Example 1
Input
rooms = [[2147483647,-1,0,2147483647],[2147483647,2147483647,2147483647,-1],[2147483647,-1,2147483647,-1],[0,-1,2147483647,2147483647]]
Output
[[3,-1,0,1],[2,2,1,-1],[1,-1,2,-1],[0,-1,3,4]]
Explanation

The empty rooms are filled with the shortest distance to their nearest gate.

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 리소스

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