๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

[leetcode][Hard][Two Pointers] 42. Trapping Rain Water ํ’€์ด, ํ•ด์„ค (python) : ํ•ด์„ค ์ฐธ๊ณ ํ•ด์„œ ํ‘ผ ํ’€์ด

ttoance 2023. 10. 29. 22:52

https://leetcode.com/problems/trapping-rain-water/

 

Trapping Rain Water - LeetCode

Can you solve this real interview question? Trapping Rain Water - Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.   Example 1: [https://assets.leetcode.com/upl

leetcode.com

 

ํ•œ์‹œ๊ฐ„ ์ •๋„ ํ’€๋ฆด๊นŒ ๋ง๊นŒ ํ•˜๋‹ค๊ฐ€, ํ’€๋ฆฌ์ง€ ์•Š์•„์„œ ... 

๊ฒฐ๊ตญ ํ•ด์„ค์„ ๋ณธ ํ’€์ด์ด๋‹ค. 

 

์ผ๋‹จ, trap์„ ๊ณ„์‚ฐํ• ๋•Œ 2๊ฐ€์ง€ ์ผ€์ด์Šค๊ฐ€ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ํ™•์ธํ–ˆ๋‹ค. 

๋ฌผ์ด ๊ฐ‡ํžˆ๋Š” ๊ตฌ์—ญ์„ ์ž˜ ๋‚˜๋ˆด๋‹ค๋Š” ๊ฐ€์ •ํ•˜์—, ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์˜ ์ตœ์†Œ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

2๊ฐ€์ง€ ์ผ€์ด์Šค : min(l,h) - h[i]

๊ทธ๋ž˜์„œ ์˜์—ญ์„ ์ž˜ ๋‚˜๋ˆ„๊ธฐ๋งŒ ํ•˜๋ฉด ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ, ๊ทธ ๋ถ€๋ถ„์—์„œ ๋ญ”๊ฐ€ ์ฝ”๋“œ๊ฐ€ ๊ผฌ์˜€๋Š”์ง€ ํ•ด๊ฒฐ์ด ์–ด๋ ค์› ๋‹ค. 

ํ’€๋‹ค๊ฐ€ ์ค‘๋„ํฌ๊ธฐํ•œ ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ก์ฐจ ๊ณต์œ ํ•œ๋‹ค. 

class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        
        limit = height[0]
        start = 0 
        trap = 0
        for end in range(1, len(height) - 1):
            h = height[end]
            if h > limit:
                length = min(height[start], height[end])
                t = end - 1
                while t >= start:
                    if length > height[t]:
                        trap += (length - height[t])
                    print('..', t, trap, length)
                    t -= 1 
                
                # ๋‹ค์Œ ๋ชฉ๋ก ์กฐํšŒ 
                start = end + 1
                limit = height[start]
            else : 
                # ๋‹ค์Œ ๋†’์ด๋กœ ์ด๋™ 
                limit = h
            
            # print(end, trap)
            
            
                
        
        return trap

 

ํ•ด์„ค

https://www.youtube.com/watch?v=ZI2z5pq0TqA

class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """

        if not height:
            return 0 
        
        l, r = 0, len(height) - 1
        leftMax, rightMax = height[l], height[r]
        res = 0 
        
        while l < r:
            if leftMax < rightMax:
                l += 1
                leftMax = max(leftMax, height[l])
                res += leftMax - height[l]
            else:
                r -= 1 
                rightMax = max(rightMax, height[r])
                res += rightMax - height[r]
            
    
        
        return res

ํ•ด์„ค์€ ๊ฐ๊ฐ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์˜ minLeft์™€ maxRight๋ฅผ ๊ณ„์‚ฐํ•ด์„œ, ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๋ฉด์„œ ๊ณ„์‚ฐํ–ˆ๋‹ค. 

chatgpt ํ†ตํ•ด์„œ for๋ฌธ์œผ๋กœ ๋ฐ”๊พธ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ’€์ด๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ์‚ฌ์‹ค์ƒ for๋ฌธ์— ์‚ฌ์šฉํ•œ i๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. 

left, right = 0, len(height) - 1
leftMax, rightMax = height[left], height[right]
res = 0

for i in range(len(height)):
    if leftMax < rightMax:
        leftMax = max(leftMax, height[left])
        res += leftMax - height[left]
        left += 1
    else:
        rightMax = max(rightMax, height[right])
        res += rightMax - height[right]
        right -= 1

 

์•„๋ž˜์™€ ๊ฐ™์ด ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ดํ•ดํ•˜๋ฉด ์ข€ ๋” ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

์•„๋ฌด๋ž˜๋„ Hard๋‚œ์ด๋„ ๋ฌธ์ œ๋Š” ์•„์ง ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค. 

๋‹ค์Œ๋ฒˆ์— ๋‹ค์‹œ ์‹œ๋„ํ•ด๋ด์•ผ๊ฒ ๋‹ค. 

๋ฐ˜์‘ํ˜•