From 6e12f1f75e530a8ca7ad2e0ac9dc4ea2523139f1 Mon Sep 17 00:00:00 2001 From: Saurabh Kokate Date: Mon, 6 Oct 2025 10:03:05 +0530 Subject: [PATCH 1/4] Create swim_in_rising_water.py --- matrix/swim_in_rising_water.py | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 matrix/swim_in_rising_water.py diff --git a/matrix/swim_in_rising_water.py b/matrix/swim_in_rising_water.py new file mode 100644 index 000000000000..508fda47f5f6 --- /dev/null +++ b/matrix/swim_in_rising_water.py @@ -0,0 +1,62 @@ +import heapq +from typing import List + +def swim_in_rising_water(grid: List[List[int]]) -> int: + """ + Return the minimum time to reach the bottom right square of the grid from the top left, + where time t allows swimming to cells with elevation <= t. + + This is a variant of Dijkstra's shortest path algorithm using a priority queue (min-heap) + to find the minimum elevation (time) path in a grid graph. + + Reference: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm + + :param grid: n x n integer matrix where grid[i][j] is the elevation at (i, j) + :return: Minimum time to reach (n-1, n-1) from (0, 0) + + Examples: + >>> swim_in_rising_water([[0, 2], [1, 3]]) + 3 + >>> swim_in_rising_water([[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]) + 16 + >>> swim_in_rising_water([[0]]) # n=1 edge case + 0 + >>> swim_in_rising_water([[5, 3], [4, 2]]) # Another small grid + 4 + """ + if not grid or not grid[0]: + raise ValueError("Grid must be a non-empty n x n matrix") + + n = len(grid) + if n != len(grid[0]): + raise ValueError("Grid must be square (n x n)") + + # Directions: right, down, left, up + directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] + + # Min-heap: (max_elevation_so_far, row, col) + min_heap: List[tuple[int, int, int]] = [(grid[0][0], 0, 0)] + visited = [[False] * n for _ in range(n)] + visited[0][0] = True + + while min_heap: + max_elev, r, c = heapq.heappop(min_heap) + + # Reached bottom-right + if r == n - 1 and c == n - 1: + return max_elev + + for dr, dc in directions: + nr, nc = r + dr, c + dc + if 0 <= nr < n and 0 <= nc < n and not visited[nr][nc]: + visited[nr][nc] = True + # The time is the max elevation encountered on this path + new_elev = max(max_elev, grid[nr][nc]) + heapq.heappush(min_heap, (new_elev, nr, nc)) + + # Should always reach if grid is valid, but for completeness + raise ValueError("No path found to bottom-right (grid constraints violated)") + +if __name__ == "__main__": + import doctest + doctest.testmod() From 680247716560e5c675b7b0c76c3df6328cbfd5fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 04:34:22 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/swim_in_rising_water.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/matrix/swim_in_rising_water.py b/matrix/swim_in_rising_water.py index 508fda47f5f6..6650feb43056 100644 --- a/matrix/swim_in_rising_water.py +++ b/matrix/swim_in_rising_water.py @@ -1,6 +1,7 @@ import heapq from typing import List + def swim_in_rising_water(grid: List[List[int]]) -> int: """ Return the minimum time to reach the bottom right square of the grid from the top left, @@ -57,6 +58,8 @@ def swim_in_rising_water(grid: List[List[int]]) -> int: # Should always reach if grid is valid, but for completeness raise ValueError("No path found to bottom-right (grid constraints violated)") + if __name__ == "__main__": import doctest + doctest.testmod() From 6168d6d3745ce5936b743f452bceb8341c85dcd5 Mon Sep 17 00:00:00 2001 From: Saurabh Kokate Date: Mon, 6 Oct 2025 10:13:33 +0530 Subject: [PATCH 3/4] Delete matrix/swim_in_rising_water.py --- matrix/swim_in_rising_water.py | 65 ---------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 matrix/swim_in_rising_water.py diff --git a/matrix/swim_in_rising_water.py b/matrix/swim_in_rising_water.py deleted file mode 100644 index 6650feb43056..000000000000 --- a/matrix/swim_in_rising_water.py +++ /dev/null @@ -1,65 +0,0 @@ -import heapq -from typing import List - - -def swim_in_rising_water(grid: List[List[int]]) -> int: - """ - Return the minimum time to reach the bottom right square of the grid from the top left, - where time t allows swimming to cells with elevation <= t. - - This is a variant of Dijkstra's shortest path algorithm using a priority queue (min-heap) - to find the minimum elevation (time) path in a grid graph. - - Reference: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm - - :param grid: n x n integer matrix where grid[i][j] is the elevation at (i, j) - :return: Minimum time to reach (n-1, n-1) from (0, 0) - - Examples: - >>> swim_in_rising_water([[0, 2], [1, 3]]) - 3 - >>> swim_in_rising_water([[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]) - 16 - >>> swim_in_rising_water([[0]]) # n=1 edge case - 0 - >>> swim_in_rising_water([[5, 3], [4, 2]]) # Another small grid - 4 - """ - if not grid or not grid[0]: - raise ValueError("Grid must be a non-empty n x n matrix") - - n = len(grid) - if n != len(grid[0]): - raise ValueError("Grid must be square (n x n)") - - # Directions: right, down, left, up - directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] - - # Min-heap: (max_elevation_so_far, row, col) - min_heap: List[tuple[int, int, int]] = [(grid[0][0], 0, 0)] - visited = [[False] * n for _ in range(n)] - visited[0][0] = True - - while min_heap: - max_elev, r, c = heapq.heappop(min_heap) - - # Reached bottom-right - if r == n - 1 and c == n - 1: - return max_elev - - for dr, dc in directions: - nr, nc = r + dr, c + dc - if 0 <= nr < n and 0 <= nc < n and not visited[nr][nc]: - visited[nr][nc] = True - # The time is the max elevation encountered on this path - new_elev = max(max_elev, grid[nr][nc]) - heapq.heappush(min_heap, (new_elev, nr, nc)) - - # Should always reach if grid is valid, but for completeness - raise ValueError("No path found to bottom-right (grid constraints violated)") - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 5c21520007f40cd1944dcb9eed3856e0821d6122 Mon Sep 17 00:00:00 2001 From: Saurabh Kokate Date: Mon, 6 Oct 2025 10:14:33 +0530 Subject: [PATCH 4/4] Create swim_in_rising_water.py --- graphs/swim_in_rising_water.py | 74 ++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 graphs/swim_in_rising_water.py diff --git a/graphs/swim_in_rising_water.py b/graphs/swim_in_rising_water.py new file mode 100644 index 000000000000..a681c0d2686f --- /dev/null +++ b/graphs/swim_in_rising_water.py @@ -0,0 +1,74 @@ +import heapq + + +def swim_in_rising_water(grid: list[list[int]]) -> int: + """ + Return the minimum time to reach the bottom right square of the grid + from the top left, where time t allows swimming to cells with + elevation <= t. + + This is a variant of Dijkstra's shortest path algorithm using a + priority queue (min-heap) to find the minimum elevation (time) path + in a grid graph. + + Reference: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm + + :param grid: n x n integer matrix where grid[i][j] is the elevation + at (i, j) + :return: Minimum time to reach (n-1, n-1) from (0, 0) + + Examples: + >>> swim_in_rising_water([[0, 2], [1, 3]]) + 3 + >>> grid = [ + ... [0, 1, 2, 3, 4], + ... [24, 23, 22, 21, 5], + ... [12, 13, 14, 15, 16], + ... [11, 17, 18, 19, 20], + ... [10, 9, 8, 7, 6] + ... ] + >>> swim_in_rising_water(grid) + 16 + >>> swim_in_rising_water([[0]]) # n=1 edge case + 0 + >>> swim_in_rising_water([[5, 3], [4, 2]]) # Another small grid + 5 + """ + if not grid or not grid[0]: + raise ValueError("Grid must be a non-empty n x n matrix") + + n = len(grid) + if n != len(grid[0]): + raise ValueError("Grid must be square (n x n)") + + # Directions: right, down, left, up + directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] + + # Min-heap: (max_elevation_so_far, row, col) + min_heap: list[tuple[int, int, int]] = [(grid[0][0], 0, 0)] + visited = [[False] * n for _ in range(n)] + visited[0][0] = True + + while min_heap: + max_elev, r, c = heapq.heappop(min_heap) + + # Reached bottom-right + if r == n - 1 and c == n - 1: + return max_elev + + for dr, dc in directions: + nr, nc = r + dr, c + dc + if 0 <= nr < n and 0 <= nc < n and not visited[nr][nc]: + visited[nr][nc] = True + # The time is the max elevation encountered on this path + new_elev = max(max_elev, grid[nr][nc]) + heapq.heappush(min_heap, (new_elev, nr, nc)) + + # Should always reach if grid is valid, but for completeness + raise ValueError("No path found to bottom-right (grid constraints violated)") + + +if __name__ == "__main__": + import doctest + + doctest.testmod()