상위 150개 인터뷰쉬움

빗물 가두기

'빗물 트랩' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

각 막대의 너비가 1인 고도 지도를 나타내는 n 음수가 아닌 정수가 주어지면 비가 내린 후 얼마나 많은 물을 가둘 수 있는지 계산합니다.

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

제약
  • n == len(height)
  • 1 <= n <= 2 * 10^4
  • 0 <= height[i] <= 10^5

Example 1
Input
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output
6
Explanation

The elevation map traps 6 units of rain water. Water fills between the bars: 1 unit above index 2, 1 unit above index 4, 2 units above index 5, 1 unit above index 6, and 1 unit above index 9.

Example 2
Input
height = [4, 2, 0, 3, 2, 5]
Output
9
Explanation

Water trapped: 2 above index 1, 4 above index 2, 1 above index 3, 2 above index 4 = 9 total.

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

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