diff --git a/work/1/Medium/Array/200.number-of-islands.py b/work/1/Medium/Array/200.number-of-islands.py index 470b8e7..146eb0d 100644 --- a/work/1/Medium/Array/200.number-of-islands.py +++ b/work/1/Medium/Array/200.number-of-islands.py @@ -12,7 +12,7 @@ An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. - + Example 1: @@ -34,19 +34,39 @@ Input: grid = [ ] Output: 3 - + Constraints: - • m == grid.length + • m == grid.length - • n == grid[i].length + • n == grid[i].length - • 1 <= m, n <= 300 + • 1 <= m, n <= 300 - • grid[i][j] is '0' or '1'. + • grid[i][j] is '0' or '1'. """ + class Solution: def numIslands(self, grid: List[List[str]]) -> int: - + rows, cols = len(grid), len(grid[0]) + + def dfs(r, c): + if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != "1": + return + else: + grid[r][c] = "0" + dfs(r, c + 1) # right + dfs(r + 1, c) # bottom + dfs(r, c - 1) # left + dfs(r - 1, c) # top + + num_islands = 0 + for r in range(rows): + for c in range(cols): + if grid[r][c] == "1": + num_islands += 1 + dfs(r, c) + + return num_islands diff --git a/work/1/Medium/Array/417.pacific-atlantic-water-flow.py b/work/1/Medium/Array/417.pacific-atlantic-water-flow.py new file mode 100644 index 0000000..615f377 --- /dev/null +++ b/work/1/Medium/Array/417.pacific-atlantic-water-flow.py @@ -0,0 +1,75 @@ +""" +417. Pacific Atlantic Water Flow +Difficulty: Medium +https://leetcode.com/problems/pacific-atlantic-water-flow/ + +────────────────────────────────────────────────── + +There is an m x n rectangular island that borders both the Pacific +Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left +and top edges, and the Atlantic Ocean touches the island's right and +bottom edges. + +The island is partitioned into a grid of square cells. You are given +an m x n integer matrix heights where heights[r][c] represents the +height above sea level of the cell at coordinate (r, c). + +The island receives a lot of rain, and the rain water can flow to +neighboring cells directly north, south, east, and west if the +neighboring cell's height is less than or equal to the current cell's +height. Water can flow from any cell adjacent to an ocean into the +ocean. + +Return a 2D list of grid coordinates result where result[i] = [ri, +ci] denotes that rain water can flow from cell (ri, ci) to both the +Pacific and Atlantic oceans. + + + +Example 1: + +Input: heights = +[[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] +Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] +Explanation: The following cells can flow to the Pacific and Atlantic +oceans, as shown below: +[0,4]: [0,4] -> Pacific Ocean + [0,4] -> Atlantic Ocean +[1,3]: [1,3] -> [0,3] -> Pacific Ocean + [1,3] -> [1,4] -> Atlantic Ocean +[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean + [1,4] -> Atlantic Ocean +[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean + [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean +[3,0]: [3,0] -> Pacific Ocean + [3,0] -> [4,0] -> Atlantic Ocean +[3,1]: [3,1] -> [3,0] -> Pacific Ocean + [3,1] -> [4,1] -> Atlantic Ocean +[4,0]: [4,0] -> Pacific Ocean + [4,0] -> Atlantic Ocean +Note that there are other possible paths for these cells to flow to +the Pacific and Atlantic oceans. + +Example 2: + +Input: heights = [[1]] +Output: [[0,0]] +Explanation: The water can flow from the only cell to the Pacific and +Atlantic oceans. + + + +Constraints: + + • m == heights.length + + • n == heights[r].length + + • 1 <= m, n <= 200 + + • 0 <= heights[r][c] <= 10^5 +""" + +class Solution: + def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]: + diff --git a/work/1/Medium/Hash Table/133.clone-graph.py b/work/1/Medium/Hash Table/133.clone-graph.py new file mode 100644 index 0000000..b2d22e2 --- /dev/null +++ b/work/1/Medium/Hash Table/133.clone-graph.py @@ -0,0 +1,108 @@ +""" +133. Clone Graph +Difficulty: Medium +https://leetcode.com/problems/clone-graph/ + +────────────────────────────────────────────────── + +Given a reference of a node in a connected undirected graph. + +Return a deep copy (clone) of the graph. + +Each node in the graph contains a value (int) and a list (List[Node]) +of its neighbors. + +class Node { + public int val; + public List neighbors; +} + + + +Test case format: + +For simplicity, each node's value is the same as the node's index +(1-indexed). For example, the first node with val == 1, the second +node with val == 2, and so on. The graph is represented in the test +case using an adjacency list. + +An adjacency list is a collection of unordered lists used to +represent a finite graph. Each list describes the set of neighbors of +a node in the graph. + +The given node will always be the first node with val = 1. You must +return the copy of the given node as a reference to the cloned graph. + + + +Example 1: + +Input: adjList = [[2,4],[1,3],[2,4],[1,3]] +Output: [[2,4],[1,3],[2,4],[1,3]] +Explanation: There are 4 nodes in the graph. +1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node +(val = 4). +2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node +(val = 3). +3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node +(val = 4). +4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node +(val = 3). + +Example 2: + +Input: adjList = [[]] +Output: [[]] +Explanation: Note that the input contains one empty list. The graph +consists of only one node with val = 1 and it does not have any +neighbors. + +Example 3: + +Input: adjList = [] +Output: [] +Explanation: This an empty graph, it does not have any nodes. + + + +Constraints: + + • The number of nodes in the graph is in the range [0, 100]. + + • 1 <= Node.val <= 100 + + • Node.val is unique for each node. + + • There are no repeated edges and no self-loops in the graph. + +• The Graph is connected and all nodes can be visited starting from +the given node. +""" + +""" +# Definition for a Node. +class Node: + def __init__(self, val = 0, neighbors = None): + self.val = val + self.neighbors = neighbors if neighbors is not None else [] +""" + +from typing import Optional + + +class Solution: + def cloneGraph(self, node: Optional["Node"]) -> Optional["Node"]: + oldToNew = {} + + def dfs(node): + if node in oldToNew: + return oldToNew[node] + + copy = Node(node.val) + oldToNew[node] = copy + + for n in node.neighbors: + copy.neighbors.append(dfs(n)) + return copy + + return dfs(node) if node else None