Algorithm/LeetCode

[LeetCode] 42. Trapping Rain Water - Python

์žํ๋‹ˆ 2022. 4. 27. 15:13

๐Ÿค” ๋ฌธ์ œ


 

Trapping Rain Water - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

๐Ÿ˜€ ํ’€์ด


ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ฒฝ ๋†’์ด ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ๋น„๊ฐ€ ์˜จ ๋’ค ๋ฒฝ ์‚ฌ์ด์— ๊ณ ์ธ ๋น—๋ฌผ ์–‘์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

๊ฐ ์œ„์น˜๋งˆ๋‹ค ๊ณ ์ผ ๋น—๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ๋ฌธ์ œ ํ’€์ด๋ฅผ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ ์ด ๋•Œ ๊ณ ๋ คํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

# ํ˜„์žฌ ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ..

  1. ์ขŒ์ธก์— ์žˆ๋Š” ๋ฒฝ๋“ค ์ค‘ ์ตœ๋Œ€ ๋†’์ด
  2. ์šฐ์ธก์— ์žˆ๋Š” ๋ฒฝ๋“ค ์ค‘ ์ตœ๋Œ€ ๋†’์ด

์œ„ ๋‘ ๊ฐ€์ง€๋ฅผ ์•ˆ๋‹ค๋ฉด ๋‘ ๊ฐ€์ง€ ์ค‘ ์ตœ์†Œ ๊ฐ’์—์„œ ํ˜„์žฌ ๋†’์ด๋ฅผ ๋บ€ ์–‘์ด ๊ณ ์ด๋Š” ๋น—๋ฌผ ์–‘์ด ๋ฉ๋‹ˆ๋‹ค. 

 

์ฆ‰, ํ˜„์žฌ ์œ„์น˜ ๋†’์ด๊ฐ€ 2์ด๊ณ  ์ขŒ์ธก์ด 4, ์šฐ์ธก์ด 5๋ผ๋Š” ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ํ˜„์žฌ ์œ„์น˜์—๋Š” (4 - 2) ๋งŒํผ์˜ ๋น—๋ฌผ์ด ๊ณ ์ด๊ธฐ ๋•Œ๋ฌธ์ด์ฃ .

 

์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ ํ˜„์žฌ ๋†’์ด๊ฐ€ ์ขŒ์ธก ํ˜น์€ ์šฐ์ธก๋ณด๋‹ค ๋†’๋‹ค๋ฉด ๋น—๋ฌผ์ด ๊ณ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ•˜๋‹ค๊ฐ€ ์ข€ ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ฐพ๊ฒŒ๋˜์—ˆ๋Š”๋ฐ ๋ชจ๋“  ๋ฒฝ ์ค‘ ๊ฐ€์žฅ ๋†’์€ ๋ฒฝ ํ•˜๋‚˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์žก๋Š” ํ’€์ด ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐ€์žฅ ๋†’์€ ๋ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์ธก์„ ํ›‘์–ด๋ณด๊ณ  ๊ทธ ๋’ค ์šฐ์ธก๋„ ํ›‘์–ด๋ณด๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ˜„์žฌ ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ ๋งค๋ฒˆ ์ขŒ, ์šฐ์ธก์˜ ๋ฒฝ ์ตœ๋Œ€ ๋†’์ด๋ฅผ ๊ตฌํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ „์ฒด ํ’€์ด ์ฝ”๋“œ์˜ ์ผ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

max_height = max(height)
max_place = height.index(max_height)

left_max = height[0]

# left ๋Œ๊ธฐ
for i in range(max_place):
    left_max = max(left_max, height[i])
    answer += left_max - height[i]

 

์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๊ฒŒ ๋˜๋ฉด ๊ฐ€์žฅ ๋†’์€ ๋ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์ธก์„ ํ›‘์–ด๋ณด๋Š” ๊ณผ์ •์ธ๋ฐ ์ขŒ์ธก ๋ฒฝ์˜ ์ตœ๋Œ€ ๋†’์ด๋ฅผ ํ˜„์žฌ ์œ„์น˜๋ฅผ ์‚ดํŽด๊ฐ€๋ฉฐ ๊ฐฑ์‹ ํ•ด๋‚˜๊ฐ€๋Š” ๊ณผ์ •๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. for๋ฌธ ์•ˆ์˜ left_max์—์„œ ๊ฐฑ์‹ ํ•˜๊ณ  ์žˆ์ฃ !

 

 ๋˜ํ•œ left_max - height[i], ์ฆ‰ ์ขŒ์ธก ๋ฒฝ์˜ ์ตœ๋Œ€ ๋†’์ด์™€ ํ˜„์žฌ ๋†’์ด์˜ ์ฐจ๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ์œ„์น˜์— ๊ณ ์ธ ๋น—๋ฌผ ์–‘์„ ๊ตฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์„ ํ†ตํ•ด for๋ฌธ์ด ๋๋‚œ๋‹ค๋ฉด ๊ฐ€์žฅ ๋†’์€ ๋ฒฝ์„ ๊ธฐ์ค€ ์ขŒ์ธก์— ๊ณ ์ธ ๋น—๋ฌผ ์–‘์ด ์ „๋ถ€ ๊ตฌํ•ด์ง‘๋‹ˆ๋‹ค.

 

์ด์ œ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์šฐ์ธก์„ ์‚ดํŽด๋ณธ๋‹ค๋ฉด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


๐Ÿ’ป ํ’€์ด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

class Solution:
    def trap(self, height: List[int]) -> int:
        answer = 0
        max_height = max(height)
        max_place = height.index(max_height)
        
        left_max = height[0]
        right_max = height[-1]
        
        # left ๋Œ๊ธฐ
        for i in range(max_place):
            left_max = max(left_max, height[i])
            answer += left_max - height[i]
        
        # right ๋Œ๊ธฐ
        for i in reversed(range(max_place, len(height))):
            right_max = max(right_max, height[i])      
            answer += right_max - height[i]      
        
        return answer

 

๋ฐ˜์‘ํ˜•