Skip to content

Commit 359666e

Browse files
committed
feat: add solutions to lcci problem: No.08.10
No.08.10.Color Fill
1 parent a302cfc commit 359666e

File tree

12 files changed

+341
-57
lines changed

12 files changed

+341
-57
lines changed

lcci/08.10.Color Fill/README.md

+61-14
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,26 @@ sr = 1, sc = 1, newColor = 2
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36+
DFS。
37+
3638
<!-- tabs:start -->
3739

3840
### **Python3**
3941

4042
<!-- 这里可写当前语言的特殊实现逻辑 -->
4143

4244
```python
43-
45+
class Solution:
46+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
47+
def dfs(i, j, oc, nc):
48+
if i < 0 or i >= len(image) or j < 0 or j >= len(image[0]) or image[i][j] != oc or image[i][j] == nc:
49+
return
50+
image[i][j] = nc
51+
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
52+
dfs(i + x, j + y, oc, nc)
53+
54+
dfs(sr, sc, image[sr][sc], newColor)
55+
return image
4456
```
4557

4658
### **Java**
@@ -49,30 +61,65 @@ sr = 1, sc = 1, newColor = 2
4961

5062
```java
5163
class Solution {
64+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
65+
5266
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
53-
int oldColor = image[sr][sc];
54-
dfs(image, sr, sc, oldColor, newColor);
67+
dfs(image, sr, sc, image[sr][sc], newColor);
5568
return image;
5669
}
5770

58-
private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
59-
if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length) {
71+
private void dfs(int[][] image, int i, int j, int oc, int nc) {
72+
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc || image[i][j] == nc) {
6073
return;
6174
}
62-
63-
int color = image[sr][sc];
64-
if (color != newColor && color == oldColor) {
65-
image[sr][sc] = newColor;
66-
// up down left right
67-
dfs(image, sr, sc + 1, oldColor, newColor);
68-
dfs(image, sr, sc - 1, oldColor, newColor);
69-
dfs(image, sr + 1, sc, oldColor, newColor);
70-
dfs(image, sr - 1, sc, oldColor, newColor);
75+
image[i][j] = nc;
76+
for (int[] dir : dirs) {
77+
dfs(image, i + dir[0], j + dir[1], oc, nc);
7178
}
7279
}
7380
}
7481
```
7582

83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
89+
90+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
91+
dfs(image, sr, sc, image[sr][sc], newColor);
92+
return image;
93+
}
94+
95+
void dfs(vector<vector<int>>& image, int i, int j, int oc, int nc) {
96+
if (i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] != oc || image[i][j] == nc) return;
97+
image[i][j] = nc;
98+
for (auto& dir : dirs) dfs(image, i + dir[0], j + dir[1], oc, nc);
99+
}
100+
};
101+
```
102+
103+
### **Go**
104+
105+
```go
106+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
107+
dfs(image, sr, sc, image[sr][sc], newColor)
108+
return image
109+
}
110+
111+
func dfs(image [][]int, i, j, oc, nc int) {
112+
if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oc || image[i][j] == nc {
113+
return
114+
}
115+
image[i][j] = nc
116+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
117+
for _, dir := range dirs {
118+
dfs(image, i+dir[0], j+dir[1], oc, nc)
119+
}
120+
}
121+
```
122+
76123
### **...**
77124

78125
```

lcci/08.10.Color Fill/README_EN.md

+61-14
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,89 @@ to the starting pixel.</pre>
3838

3939
## Solutions
4040

41+
DFS.
42+
4143
<!-- tabs:start -->
4244

4345
### **Python3**
4446

4547
```python
46-
48+
class Solution:
49+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
50+
def dfs(i, j, oc, nc):
51+
if i < 0 or i >= len(image) or j < 0 or j >= len(image[0]) or image[i][j] != oc or image[i][j] == nc:
52+
return
53+
image[i][j] = nc
54+
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
55+
dfs(i + x, j + y, oc, nc)
56+
57+
dfs(sr, sc, image[sr][sc], newColor)
58+
return image
4759
```
4860

4961
### **Java**
5062

5163
```java
5264
class Solution {
65+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
66+
5367
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
54-
int oldColor = image[sr][sc];
55-
dfs(image, sr, sc, oldColor, newColor);
68+
dfs(image, sr, sc, image[sr][sc], newColor);
5669
return image;
5770
}
5871

59-
private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
60-
if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length) {
72+
private void dfs(int[][] image, int i, int j, int oc, int nc) {
73+
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc || image[i][j] == nc) {
6174
return;
6275
}
63-
64-
int color = image[sr][sc];
65-
if (color != newColor && color == oldColor) {
66-
image[sr][sc] = newColor;
67-
// up down left right
68-
dfs(image, sr, sc + 1, oldColor, newColor);
69-
dfs(image, sr, sc - 1, oldColor, newColor);
70-
dfs(image, sr + 1, sc, oldColor, newColor);
71-
dfs(image, sr - 1, sc, oldColor, newColor);
76+
image[i][j] = nc;
77+
for (int[] dir : dirs) {
78+
dfs(image, i + dir[0], j + dir[1], oc, nc);
7279
}
7380
}
7481
}
7582
```
7683

84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
90+
91+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
92+
dfs(image, sr, sc, image[sr][sc], newColor);
93+
return image;
94+
}
95+
96+
void dfs(vector<vector<int>>& image, int i, int j, int oc, int nc) {
97+
if (i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] != oc || image[i][j] == nc) return;
98+
image[i][j] = nc;
99+
for (auto& dir : dirs) dfs(image, i + dir[0], j + dir[1], oc, nc);
100+
}
101+
};
102+
```
103+
104+
### **Go**
105+
106+
```go
107+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
108+
dfs(image, sr, sc, image[sr][sc], newColor)
109+
return image
110+
}
111+
112+
func dfs(image [][]int, i, j, oc, nc int) {
113+
if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oc || image[i][j] == nc {
114+
return
115+
}
116+
image[i][j] = nc
117+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
118+
for _, dir := range dirs {
119+
dfs(image, i+dir[0], j+dir[1], oc, nc)
120+
}
121+
}
122+
```
123+
77124
### **...**
78125

79126
```

lcci/08.10.Color Fill/Solution.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
4+
5+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
6+
dfs(image, sr, sc, image[sr][sc], newColor);
7+
return image;
8+
}
9+
10+
void dfs(vector<vector<int>>& image, int i, int j, int oc, int nc) {
11+
if (i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] != oc || image[i][j] == nc) return;
12+
image[i][j] = nc;
13+
for (auto& dir : dirs) dfs(image, i + dir[0], j + dir[1], oc, nc);
14+
}
15+
};

lcci/08.10.Color Fill/Solution.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
2+
dfs(image, sr, sc, image[sr][sc], newColor)
3+
return image
4+
}
5+
6+
func dfs(image [][]int, i, j, oc, nc int) {
7+
if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oc || image[i][j] == nc {
8+
return
9+
}
10+
image[i][j] = nc
11+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
12+
for _, dir := range dirs {
13+
dfs(image, i+dir[0], j+dir[1], oc, nc)
14+
}
15+
}

lcci/08.10.Color Fill/Solution.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
class Solution {
2+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
3+
24
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
3-
int oldColor = image[sr][sc];
4-
dfs(image, sr, sc, oldColor, newColor);
5+
dfs(image, sr, sc, image[sr][sc], newColor);
56
return image;
67
}
78

8-
private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
9-
if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length) {
9+
private void dfs(int[][] image, int i, int j, int oc, int nc) {
10+
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc || image[i][j] == nc) {
1011
return;
1112
}
12-
13-
int color = image[sr][sc];
14-
if (color != newColor && color == oldColor) {
15-
image[sr][sc] = newColor;
16-
// up down left right
17-
dfs(image, sr, sc + 1, oldColor, newColor);
18-
dfs(image, sr, sc - 1, oldColor, newColor);
19-
dfs(image, sr + 1, sc, oldColor, newColor);
20-
dfs(image, sr - 1, sc, oldColor, newColor);
13+
image[i][j] = nc;
14+
for (int[] dir : dirs) {
15+
dfs(image, i + dir[0], j + dir[1], oc, nc);
2116
}
2217
}
2318
}

lcci/08.10.Color Fill/Solution.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
3+
def dfs(i, j, oc, nc):
4+
if i < 0 or i >= len(image) or j < 0 or j >= len(image[0]) or image[i][j] != oc or image[i][j] == nc:
5+
return
6+
image[i][j] = nc
7+
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
8+
dfs(i + x, j + y, oc, nc)
9+
10+
dfs(sr, sc, image[sr][sc], newColor)
11+
return image
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [2098. Subsequence of Size K With the Largest Even Sum](https://leetcode-cn.com/problems/subsequence-of-size-k-with-the-largest-even-sum)
2+
3+
[English Version](/solution/2000-2099/2098.Subsequence%20of%20Size%20K%20With%20the%20Largest%20Even%20Sum/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Find the <strong>largest even sum</strong> of any subsequence of <code>nums</code> that has a length of <code>k</code>.</p>
10+
11+
<p>Return <em>this sum, or </em><code>-1</code><em> if such a sum does not exist</em>.</p>
12+
13+
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [4,1,5,3,1], k = 3
20+
<strong>Output:</strong> 12
21+
<strong>Explanation:</strong>
22+
The subsequence with the largest possible even sum is [4,5,3]. It has a sum of 4 + 5 + 3 = 12.
23+
</pre>
24+
25+
<p><strong>Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> nums = [4,6,2], k = 3
29+
<strong>Output:</strong> 12
30+
<strong>Explanation:</strong>
31+
The subsequence with the largest possible even sum is [4,6,2]. It has a sum of 4 + 6 + 2 = 12.
32+
</pre>
33+
34+
<p><strong>Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [1,3,5], k = 1
38+
<strong>Output:</strong> -1
39+
<strong>Explanation:</strong>
40+
No subsequence of nums with length 1 has an even sum.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
48+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
49+
<li><code>1 &lt;= k &lt;= nums.length</code></li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
64+
```
65+
66+
### **Java**
67+
68+
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
70+
```java
71+
72+
```
73+
74+
### **TypeScript**
75+
76+
<!-- 这里可写当前语言的特殊实现逻辑 -->
77+
78+
```ts
79+
80+
```
81+
82+
### **...**
83+
84+
```
85+
86+
```
87+
88+
<!-- tabs:end -->

0 commit comments

Comments
 (0)