Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 778 · Minimum Spanning Tree

Swim in Rising Water

LeetCode 778 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Min-heap of max-height-so-far along the path.

கடினம்ArrayBinary SearchDepth-First SearchBreadth-First SearchUnion-Find
LeetCode இல் திற ↗

நான் Swim in Rising Water (LeetCode 778) ஐ இப்படித் தீர்த்தேன் — Minimum Spanning Tree பேட்டர்னின் ஒரு கடினம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

You can cross a cell once the water level reaches its height, so a path's time is the highest cell on it. Expand cells in order of that bottleneck with a min-heap (Prim/Dijkstra style); when the bottom-right is popped, its value is the answer.

Approach

  1. 1

    Heap starts with (grid[0][0], 0, 0).

  2. 2

    Pop the lowest; target → return its time.

  3. 3

    Skip visited cells; push neighbours with max(time, height).

நான் எப்படித் தீர்த்தேன்

Dijkstra / Prim on bottleneckPythonO(n² log n) time
#TC - N^2logN SC - N^2
class Solution:
    def swimInWater(self, grid: List[List[int]]) -> int:
        #prims algo
        #minimum edge weights to reach the destination
        min_heap = [(grid[0][0],0,0)] #time,row,col
        visited = set()
        n = len(grid)
        di = [0,+1,0,-1]
        dj = [+1,0,-1,0]
        while min_heap: #Maximum it can visit N^2 cells
            time,row,col = heapq.heappop(min_heap) #logN
            if row==n-1 and col==n-1:
                return time
            if (row,col) in visited:
                continue
            visited.add((row,col))
            for k in range(4):
                nrow = row+di[k]
                ncol = col+dj[k]
                if 0<=nrow<n and 0<=ncol<n and (nrow,ncol) not in visited:
                    heapq.heappush(min_heap,(max(time,grid[nrow][ncol]),nrow,ncol))
        return -1
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Swim in Rising Water — அடிக்கடி கேட்கப்படும் கேள்விகள்

Swim in Rising Water பின்னாலுள்ள intuition என்ன?

You can cross a cell once the water level reaches its height, so a path's time is the highest cell on it. Expand cells in order of that bottleneck with a min-heap (Prim/Dijkstra style); when the bottom-right is popped, its value is the answer.

Swim in Rising Water (LeetCode 778) ஐ எப்படித் தீர்ப்பது?

1. Heap starts with (grid[0][0], 0, 0). 2. Pop the lowest; target → return its time. 3. Skip visited cells; push neighbours with max(time, height).

Swim in Rising Water இன் time மற்றும் space complexity என்ன?

Time: O(n² log n). Space: O(n²). (Accepted approach: Dijkstra / Prim on bottleneck.)

Swim in Rising Water இல் பொதுவான தவறுகள் என்ன?

Binary search on time + BFS also works.