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: lcci/08.10.Color Fill/README_EN.md
+57-6
Original file line number
Diff line number
Diff line change
@@ -38,13 +38,11 @@ to the starting pixel.</pre>
38
38
39
39
## Solutions
40
40
41
-
### Solution 1: Flood Fill Algorithm
41
+
### Solution 1: DFS
42
42
43
-
The Flood Fill algorithm is a classic algorithm used to extract several connected points from a region and distinguish them from other adjacent regions (or color them differently). It is named for its strategy, which is similar to a flood spreading from one area to all reachable areas.
43
+
We design a function $dfs(i, j)$ to start filling color from $(i, j)$. If $(i, j)$ is not within the image range, or the color of $(i, j)$ is not the original color, or the color of $(i, j)$ has been filled with the new color, then return. Otherwise, fill the color of $(i, j)$ with the new color, and then recursively search the four directions: up, down, left, and right of $(i, j)$.
44
44
45
-
The simplest implementation method is to use the recursive method of DFS, or it can be implemented iteratively using BFS.
46
-
47
-
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the image, respectively.
45
+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns in the image, respectively.
We can use the method of breadth-first search. Starting from the initial point, fill the color of the initial point with the new color, and then add the initial point to the queue. Each time a point is taken from the queue, the points in the four directions: up, down, left, and right are added to the queue, until the queue is empty.
207
+
208
+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns in the image, respectively.
Copy file name to clipboardExpand all lines: lcci/08.11.Coin/README_EN.md
+4-6
Original file line number
Diff line number
Diff line change
@@ -75,8 +75,6 @@ The final answer is $f[4][n]$.
75
75
76
76
The time complexity is $O(C \times n)$, and the space complexity is $O(C \times n)$, where $C$ is the number of types of coins.
77
77
78
-
We notice that the calculation of $f[i][j]$ is only related to $f[i−1][..]$, so we can remove the first dimension and optimize the space complexity to $O(n)$.
79
-
80
78
<!-- tabs:start -->
81
79
82
80
```python
@@ -161,9 +159,7 @@ func waysToChange(n int) int {
We notice that the calculation of $f[i][j]$ is only related to $f[i−1][..]$. Therefore, we can remove the first dimension and optimize the space complexity to $O(n)$.
0 commit comments