[백준] 14719번 빗물

거북이·2023년 3월 5일
0

백준[골드5]

목록 보기
41/82
post-thumbnail

💡문제접근

  • 1열과 마지막열에는 물이 고일 수 없다.
  • 블록의 높이가 각각 다르기 때문에 구간별 블록의 높이의 최댓값을 구해준 다음 물이 고일 수 있는 부분의 값을 구해준다.

💡코드(메모리 : 31256KB, 시간 : 48ms)

import sys
input = sys.stdin.readline

H, W = map(int, input().strip().split())
block_height = list(map(int, input().strip().split()))

ans = 0
for i in range(1, W - 1):
    left_Max = max(block_height[i:])
    right_Max = max(block_height[:i + 1])
    Min_height = min(left_Max, right_Max)

    if Min_height - block_height[i] <= 0:
        continue
    else:
        ans += (Min_height - block_height[i])
print(ans)

💡소요시간 : 17m

0개의 댓글