Skip to content

Commit 0e7f8de

Browse files
author
Aarzoo
committed
Add Solutions, Explanation and Update the README File of the day 5658
1 parent e586271 commit 0e7f8de

File tree

8 files changed

+242
-0
lines changed

8 files changed

+242
-0
lines changed

Algorithms/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
| 2559 | 2559. Count Vowel Strings in Ranges | [C++](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.cpp), [Java](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.java), [JavaScript](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.js), [Python](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.py), [Go](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.go) | [Explanation](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Explanation/explanation.md) | Medium |
8888
| 2563 | Count the Number of Fair Pairs | [C++](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.cpp), [Java](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.java), [JavaScript](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.js), [Python](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.py), [Go](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.go) | [Explanation](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Explanation/explanation.md) | Medium |
8989
| 2601 | Prime Subtraction Operation | [C++](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.cpp), [Java](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.java), [JavaScript](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.js), [Python](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.py), [Go](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.go) | [Explanation](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Explanation/explanation.md) | Medium |
90+
| 2658 | Maximum Number of Fish in a Grid | [C++](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.cpp), [Java](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.java), [JavaScript](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.js), [Python](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.py), [Go](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.go) | [Explanation](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Explanation/explanation.md) | Medium |
9091
| 2661 | First Completely Painted Row or Column | [C++](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.cpp), [Java](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.java), [JavaScript](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.js), [Python](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.py), [Go](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.go) | [Explanation](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Explanation/explanation.md) | Medium |
9192
| 2678 | Number of Senior Citizens | [C++](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.cpp), [Java](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.java), [JavaScript](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.js), [Python](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.py), [Go](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.go) | [Explanation](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Explanation/explanation.md) | Easy |
9293
| 2683 | Neighboring Bitwise XOR | [C++](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.cpp), [Java](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.java), [JavaScript](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.js), [Python](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.py), [Go](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.go) | [Explanation](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Explanation/explanation.md) | Medium |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int findMaxFish(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
vector<vector<bool>> visited(m, vector<bool>(n, false));
6+
int maxFish = 0;
7+
8+
auto dfs = [&](int r, int c, auto& dfs) -> int {
9+
if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || grid[r][c] == 0)
10+
return 0;
11+
visited[r][c] = true;
12+
int fish = grid[r][c];
13+
fish += dfs(r + 1, c, dfs);
14+
fish += dfs(r - 1, c, dfs);
15+
fish += dfs(r, c + 1, dfs);
16+
fish += dfs(r, c - 1, dfs);
17+
return fish;
18+
};
19+
20+
for (int i = 0; i < m; ++i) {
21+
for (int j = 0; j < n; ++j) {
22+
if (!visited[i][j] && grid[i][j] > 0) {
23+
maxFish = max(maxFish, dfs(i, j, dfs));
24+
}
25+
}
26+
}
27+
return maxFish;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
func findMaxFish(grid [][]int) int {
2+
m, n := len(grid), len(grid[0])
3+
visited := make([][]bool, m)
4+
for i := range visited {
5+
visited[i] = make([]bool, n)
6+
}
7+
maxFish := 0
8+
9+
var dfs func(r, c int) int
10+
dfs = func(r, c int) int {
11+
if r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || grid[r][c] == 0 {
12+
return 0
13+
}
14+
visited[r][c] = true
15+
fish := grid[r][c]
16+
fish += dfs(r+1, c)
17+
fish += dfs(r-1, c)
18+
fish += dfs(r, c+1)
19+
fish += dfs(r, c-1)
20+
return fish
21+
}
22+
23+
for i := 0; i < m; i++ {
24+
for j := 0; j < n; j++ {
25+
if !visited[i][j] && grid[i][j] > 0 {
26+
maxFish = max(maxFish, dfs(i, j))
27+
}
28+
}
29+
}
30+
return maxFish
31+
}
32+
33+
func max(a, b int) int {
34+
if a > b {
35+
return a
36+
}
37+
return b
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int findMaxFish(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
boolean[][] visited = new boolean[m][n];
5+
int maxFish = 0;
6+
7+
int dfs(int r, int c) {
8+
if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || grid[r][c] == 0)
9+
return 0;
10+
visited[r][c] = true;
11+
int fish = grid[r][c];
12+
fish += dfs(r + 1, c);
13+
fish += dfs(r - 1, c);
14+
fish += dfs(r, c + 1);
15+
fish += dfs(r, c - 1);
16+
return fish;
17+
}
18+
19+
for (int i = 0; i < m; i++) {
20+
for (int j = 0; j < n; j++) {
21+
if (!visited[i][j] && grid[i][j] > 0) {
22+
maxFish = Math.max(maxFish, dfs(i, j));
23+
}
24+
}
25+
}
26+
return maxFish;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var findMaxFish = function (grid) {
2+
const m = grid.length,
3+
n = grid[0].length;
4+
const visited = Array.from({ length: m }, () => Array(n).fill(false));
5+
let maxFish = 0;
6+
7+
const dfs = (r, c) => {
8+
if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || grid[r][c] === 0)
9+
return 0;
10+
visited[r][c] = true;
11+
let fish = grid[r][c];
12+
fish += dfs(r + 1, c);
13+
fish += dfs(r - 1, c);
14+
fish += dfs(r, c + 1);
15+
fish += dfs(r, c - 1);
16+
return fish;
17+
};
18+
19+
for (let i = 0; i < m; i++) {
20+
for (let j = 0; j < n; j++) {
21+
if (!visited[i][j] && grid[i][j] > 0) {
22+
maxFish = Math.max(maxFish, dfs(i, j));
23+
}
24+
}
25+
}
26+
return maxFish;
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def findMaxFish(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
visited = [[False] * n for _ in range(m)]
5+
max_fish = 0
6+
7+
def dfs(r, c):
8+
if r < 0 or c < 0 or r >= m or c >= n or visited[r][c] or grid[r][c] == 0:
9+
return 0
10+
visited[r][c] = True
11+
fish = grid[r][c]
12+
fish += dfs(r + 1, c)
13+
fish += dfs(r - 1, c)
14+
fish += dfs(r, c + 1)
15+
fish += dfs(r, c - 1)
16+
return fish
17+
18+
for i in range(m):
19+
for j in range(n):
20+
if not visited[i][j] and grid[i][j] > 0:
21+
max_fish = max(max_fish, dfs(i, j))
22+
return max_fish
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Find the Maximum Fish in a Grid
2+
3+
## Step-by-Step Explanation
4+
5+
This problem involves finding the maximum number of fish that can be collected in a grid by exploring connected components. Below is a detailed breakdown of the approach and logic used in the implementation across **C++**, **Java**, **JavaScript**, **Python**, and **Go**. Each step explains the logic for all the solutions without directly revealing the code.
6+
7+
---
8+
9+
### General Approach
10+
11+
1. **Identify Connected Components**
12+
- The problem can be visualized as identifying connected groups of grid cells containing fish (non-zero values).
13+
- Use Depth First Search (DFS) or Breadth First Search (BFS) to traverse connected cells starting from any cell with a positive value.
14+
15+
2. **Collect Fish from Connected Cells**
16+
- As you traverse each connected component, sum up the values (number of fish) in that component.
17+
18+
3. **Track the Maximum Fish Count**
19+
- Compare the total fish collected from each connected component to keep track of the maximum.
20+
21+
---
22+
23+
### C++ Code: Step-by-Step
24+
25+
1. **Grid Traversal**
26+
- Loop through each cell in the grid. If the cell contains fish (non-zero value) and hasn't been visited yet, initiate a DFS traversal.
27+
28+
2. **DFS Implementation**
29+
- Use a recursive function to traverse all connected cells, summing up the fish values.
30+
- Mark visited cells to avoid processing them again.
31+
32+
3. **Track Maximum Fish**
33+
- After completing the DFS for each component, compare the total fish count with the current maximum and update accordingly.
34+
35+
---
36+
37+
### Java Code: Step-by-Step
38+
39+
1. **Grid Traversal**
40+
- Use nested loops to go through all the grid cells. Start DFS whenever a fish-filled cell is found.
41+
42+
2. **DFS as a Helper Function**
43+
- Implement DFS recursively or with a stack. For each connected cell, add the fish value to a running total and mark it as visited.
44+
45+
3. **Compare and Update Maximum**
46+
- At the end of the traversal for each component, compare its total fish count with the global maximum.
47+
48+
---
49+
50+
### JavaScript Code: Step-by-Step
51+
52+
1. **Iterate Through the Grid**
53+
- Use nested `for` loops to inspect each cell in the grid. When a cell with fish is found, initiate a DFS.
54+
55+
2. **Handle Connected Components**
56+
- Implement a DFS function using recursion or an iterative approach (using a stack) to explore all connected cells. Add fish values and mark cells as visited.
57+
58+
3. **Update Maximum Fish Count**
59+
- After completing the DFS for one component, compare its total fish count with the global maximum and store the higher value.
60+
61+
---
62+
63+
### Python Code: Step-by-Step
64+
65+
1. **Loop Over the Grid**
66+
- Iterate through every cell using nested loops. When a cell with fish is encountered, start a DFS.
67+
68+
2. **Recursive DFS Implementation**
69+
- Use a recursive function to traverse all connected cells. Add the value of each cell to the total count and mark it as visited.
70+
71+
3. **Global Maximum Update**
72+
- At the end of each DFS, compare the total fish collected in that component with the maximum recorded so far and update as needed.
73+
74+
---
75+
76+
### Go Code: Step-by-Step
77+
78+
1. **Traverse Each Cell**
79+
- Loop over all the cells in the grid. When a cell containing fish is found, trigger a DFS.
80+
81+
2. **DFS with a Helper Function**
82+
- Implement DFS to explore connected cells. Maintain a running total of fish and ensure cells are marked as visited.
83+
84+
3. **Update Maximum Fish Count**
85+
- After completing the DFS for a component, compare its total fish count with the global maximum and update.
86+
87+
---
88+
89+
### Complexity Analysis
90+
91+
- **Time Complexity**:
92+
$$O(n \times m)$$ for all solutions, where \(n\) and \(m\) are the grid dimensions. This is because each cell is processed once during the traversal.
93+
94+
- **Space Complexity**:
95+
- For DFS: \(O(\text{stack depth})\), which is proportional to the size of the grid in the worst case.
96+
- For BFS: \(O(n \times m)\) for the queue used to store cells during traversal.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Welcome to the LeetCode Solutions repository! Here, you'll find daily solutions
8989
| 2559 | 2559. Count Vowel Strings in Ranges | [C++](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.cpp), [Java](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.java), [JavaScript](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.js), [Python](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.py), [Go](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Code/solution.go) | [Explanation](./Algorithms/src/2559.%20Count%20Vowel%20Strings%20in%20Ranges/Explanation/explanation.md) | Medium |
9090
| 2563 | Count the Number of Fair Pairs | [C++](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.cpp), [Java](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.java), [JavaScript](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.js), [Python](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.py), [Go](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Code/solution.go) | [Explanation](./Algorithms/src/2563.%20Count%20the%20Number%20of%20Fair%20Pairs/Explanation/explanation.md) | Medium |
9191
| 2601 | Prime Subtraction Operation | [C++](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.cpp), [Java](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.java), [JavaScript](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.js), [Python](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.py), [Go](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Code/solution.go) | [Explanation](./Algorithms/src/2601.%20Prime%20Subtraction%20Operation/Explanation/explanation.md) | Medium |
92+
| 2658 | Maximum Number of Fish in a Grid | [C++](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.cpp), [Java](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.java), [JavaScript](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.js), [Python](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.py), [Go](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Code/solution.go) | [Explanation](./Algorithms/src/2658.%20Maximum%20Number%20of%20Fish%20in%20a%20Grid/Explanation/explanation.md) | Medium |
9293
| 2661 | First Completely Painted Row or Column | [C++](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.cpp), [Java](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.java), [JavaScript](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.js), [Python](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.py), [Go](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Code/solution.go) | [Explanation](./Algorithms/src/2661.%20First%20Completely%20Painted%20Row%20or%20Column/Explanation/explanation.md) | Medium |
9394
| 2678 | Number of Senior Citizens | [C++](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.cpp), [Java](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.java), [JavaScript](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.js), [Python](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.py), [Go](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Code/solution.go) | [Explanation](./Algorithms/src/2678.%20Number%20of%20Senior%20Citizens/Explanation/explanation.md) | Easy |
9495
| 2683 | Neighboring Bitwise XOR | [C++](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.cpp), [Java](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.java), [JavaScript](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.js), [Python](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.py), [Go](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Code/solution.go) | [Explanation](./Algorithms/src/2683.%20Neighboring%20Bitwise%20XOR/Explanation/explanation.md) | Medium |

0 commit comments

Comments
 (0)