Skip to content

Commit e00c0f6

Browse files
authored
Adding Swim In Rising Water Problem LeetCode #776 (haoel#269)
1 parent ecb5b88 commit e00c0f6

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ LeetCode
196196
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [Python](./algorithms/python/PositionsOfLargeGroups/largeGroupPositions.py)|Easy|
197197
|820|[Short Encoding of Words](https://leetcode.com/problems/short-encoding-of-words/) | [C++](./algorithms/cpp/shortEncodingOfWords/ShortEncodingOfWords.cpp)|Medium|
198198
|804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [C++](./algorithms/cpp/uniqueMorseCodeWords/UniqueMorseCodeWords.cpp)|Easy|
199+
|776|[Swim In Rising Water](https://leetcode.com/problems/swim-in-rising-water/description/) | [Python](./algorithms/python/SwimInRisingWater/swim_in_rising_water.py)|Hard|
199200
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description) | [C++](./algorithms/cpp/jewelsAndStones/JewelsAndStones.cpp)|Easy|
200201
|747|[Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | [Python](./algorithms/python/LargestNumberAtLeastTwiceOfOthers/dominantIndex.py)|Easy|
201202
|746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [C++](./algorithms/cpp/minCostClimbingStairs/MinCostClimbingStairs.cpp), [Python](./algorithms/python/MinCostClimbingStairs/minCostClimbingStairs.py)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Solution to LeetCode problem Swim in Rising Water (#778)
3+
Difficulty: Hard
4+
5+
Explanation:
6+
We are approaching the problem through Djikstras Algorith. Essentially a BFS but with a minimum heap. The neighbors that you are adding into the queue, you are only popping the minimum value neighbor. Amongst those popped value you are checking if that value is the max value seen so far. If it is, then you have found the solution.
7+
"""
8+
from heapq import heappush, heappop
9+
from typing import List
10+
11+
class Solution:
12+
def swimInWater(self, grid: List[List[int]]) -> int:
13+
src, dst = grid[0][0], grid[len(grid)-1][len(grid)-1]
14+
visited = set()
15+
heap_queue = [(src, 0, 0)] # src, row, col
16+
output = 0
17+
directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
18+
while heap_queue:
19+
current, row, col = heappop(heap_queue)
20+
output = max(current, output)
21+
22+
# if we already hit the destination, break out of the loop
23+
if current == dst:
24+
break
25+
26+
for x, y in directions:
27+
dx, dy = row+x, col+y
28+
if self.check_bounds(dx, dy, grid) and (dx, dy) not in visited:
29+
heappush(heap_queue, (grid[dx][dy], dx, dy))
30+
visited.add((dx, dy))
31+
return output
32+
33+
def check_bounds(self, r, c, grid) -> bool:
34+
if 0 <= r < len(grid[0]) and 0 <= c < len(grid):
35+
return True
36+
return False
37+
38+
39+
if __name__ == "__main__":
40+
grid = [[0, 2], [1, 3]]
41+
print(Solution().swimInWater(grid))

0 commit comments

Comments
 (0)