You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/python/g0101_0200/s0198_house_robber/readme.md
+25-11Lines changed: 25 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -36,24 +36,38 @@ Given an integer array `nums` representing the amount of money of each house, re
36
36
*`1 <= nums.length <= 100`
37
37
*`0 <= nums[i] <= 400`
38
38
39
-
## Solution
39
+
To solve the House Robber problem, we can utilize dynamic programming to find the maximum amount of money we can rob without alerting the police. Here's how we can approach this problem:
40
+
41
+
1.**Initialize Variables**:
42
+
- Initialize two variables, `prev_max` and `curr_max`, to keep track of the maximum amount of money robbed from previous houses and the current house, respectively.
43
+
44
+
2.**Iterate Through Houses**:
45
+
- Iterate through the array of house values `nums`.
46
+
47
+
3.**Calculate Maximum Amount of Money Robbed**:
48
+
- For each house, update `curr_max` to the maximum value between the sum of the value of the current house and `prev_max`, and `prev_max`.
49
+
50
+
4.**Return Result**:
51
+
- After iterating through all houses, return `curr_max`, which represents the maximum amount of money that can be robbed without alerting the police.
This solution ensures that we calculate the maximum amount of money that can be robbed without alerting the police in linear time complexity O(n) and constant space complexity O(1), meeting the problem constraints.
Copy file name to clipboardExpand all lines: src/main/python/g0101_0200/s0200_number_of_islands/readme.md
+42-17Lines changed: 42 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -42,26 +42,51 @@ An **island** is surrounded by water and is formed by connecting adjacent lands
42
42
*`1 <= m, n <= 300`
43
43
*`grid[i][j]` is `'0'` or `'1'`.
44
44
45
-
## Solution
45
+
To solve the Number of Islands problem, we can utilize Depth-First Search (DFS) to traverse the grid and identify the islands. Here's a step-by-step approach to solve this problem:
46
+
47
+
1.**Define DFS Function**:
48
+
- Define a DFS function to traverse the grid recursively.
49
+
- This function will mark visited cells as `'0'` to avoid revisiting them.
50
+
- It will explore adjacent cells (up, down, left, right) and continue DFS traversal if the adjacent cell is land (`'1'`).
51
+
52
+
2.**Iterate Through Grid**:
53
+
- Iterate through each cell in the grid.
54
+
55
+
3.**Count Islands**:
56
+
- For each cell with land (`'1'`), call the DFS function to explore the island.
57
+
- Increment the count of islands by 1 after each DFS traversal.
58
+
59
+
4.**Return Count**:
60
+
- After traversing the entire grid, return the count of islands.
ifx<0orx>=len(grid) ory<0ory>=len(grid[0]) or grid[x][y] !='1':
67
+
defdfs(grid, i, j):
68
+
ifi<0ori>=len(grid) orj<0orj>=len(grid[0]) or grid[i][j] =='0':
52
69
return
53
-
grid[x][y] ='x'
54
-
dfs(grid, x +1, y)
55
-
dfs(grid, x -1, y)
56
-
dfs(grid, x, y +1)
57
-
dfs(grid, x, y -1)
70
+
grid[i][j] ='0'# Mark the current cell as visited
71
+
# Explore adjacent cells
72
+
dfs(grid, i+1, j)
73
+
dfs(grid, i-1, j)
74
+
dfs(grid, i, j+1)
75
+
dfs(grid, i, j-1)
58
76
59
-
islands =0
60
-
if grid and grid[0]:
61
-
for i inrange(len(grid)):
62
-
for j inrange(len(grid[0])):
63
-
if grid[i][j] =='1':
64
-
dfs(grid, i, j)
65
-
islands +=1
66
-
return islands
67
-
```
77
+
ifnot grid:
78
+
return0
79
+
80
+
num_islands =0
81
+
rows, cols =len(grid), len(grid[0])
82
+
83
+
for i inrange(rows):
84
+
for j inrange(cols):
85
+
if grid[i][j] =='1':
86
+
num_islands +=1
87
+
dfs(grid, i, j)
88
+
89
+
return num_islands
90
+
```
91
+
92
+
This solution efficiently counts the number of islands in the given grid by performing DFS traversal on each unvisited land cell. It has a time complexity of O(M * N), where M is the number of rows and N is the number of columns in the grid.
These solutions will efficiently reverse the linked list either iteratively or recursively, meeting the problem constraints. The time complexity for both approaches is O(n), where n is the number of nodes in the linked list.
To solve the Course Schedule problem, we can use a graph-based approach with topological sorting. We'll represent the courses and their prerequisites as a directed graph, and then perform a topological sort to determine if there exists any cycle in the graph. If there is a cycle, it means there is a dependency loop, and it won't be possible to complete all courses.
39
+
40
+
### Steps:
41
+
42
+
1.**Build the Graph**:
43
+
- Create an adjacency list to represent the directed graph.
44
+
- Iterate through the `prerequisites` array and add edges to the graph.
45
+
46
+
2.**Perform Topological Sorting**:
47
+
- Implement a function for topological sorting, which can be done using Depth-First Search (DFS) or Breadth-First Search (BFS).
48
+
- In each approach, keep track of the visited nodes and the current path.
49
+
- If during DFS, we encounter a node that is already in the current path, it indicates a cycle, and we return False.
50
+
- If the sorting completes without finding a cycle, return True.
51
+
52
+
3.**Check for Cycle**:
53
+
- If any node has a cycle in its path, return False.
54
+
55
+
4.**Return Result**:
56
+
- If no cycle is found, return True, indicating it's possible to finish all courses.
if colors[nei] ==self.WHITEandself.hasCycle(adj, nei, colors):
65
-
returnTrue
66
-
67
-
colors[node] =self.BLACK
68
-
returnFalse
69
-
```
94
+
### Explanation:
95
+
96
+
1.**Build the Graph**:
97
+
- We use a defaultdict to create an adjacency list representation of the directed graph.
98
+
- We iterate through the `prerequisites` array and add edges to the graph.
99
+
100
+
2.**Perform Topological Sorting**:
101
+
- We implement a function `dfs` for topological sorting using Depth-First Search (DFS).
102
+
- We keep track of visited nodes and the current path to detect cycles.
103
+
- If we encounter a node that is already in the current path, it indicates a cycle, and we return False.
104
+
- Otherwise, if DFS completes without finding a cycle, we return True.
105
+
106
+
3.**Check for Cycle**:
107
+
- We iterate through each course and perform topological sorting.
108
+
- If any node has a cycle in its path, we return False.
109
+
110
+
4.**Return Result**:
111
+
- If no cycle is found, we return True, indicating it's possible to finish all courses.
112
+
113
+
This solution has a time complexity of O(V + E), where V is the number of courses and E is the number of prerequisites. The space complexity is O(V + E) for storing the graph.
0 commit comments