💡문제접근
- 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