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: solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README_EN.md
+29-1
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,13 @@ It can be shown that all the tasks cannot be completed in fewer than 4 rounds, s
42
42
43
43
## Solutions
44
44
45
-
### Solution 1
45
+
### Solution 1: Hash Table
46
+
47
+
We use a hash table to count the number of tasks for each difficulty level. Then we traverse the hash table. For each difficulty level, if the number of tasks is $1$, then it is impossible to complete all tasks, so we return $-1$. Otherwise, we calculate the number of rounds needed to complete tasks of this difficulty level and add it to the answer.
48
+
49
+
Finally, we return the answer.
50
+
51
+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the `tasks` array.
46
52
47
53
<!-- tabs:start -->
48
54
@@ -134,6 +140,28 @@ function minimumRounds(tasks: number[]): number {
Copy file name to clipboardexpand all lines: solution/2200-2299/2245.Maximum Trailing Zeros in a Cornered Path/README_EN.md
+25-1
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,31 @@ There are no cornered paths in the grid that result in a product with a trailing
58
58
59
59
## Solutions
60
60
61
-
### Solution 1
61
+
### Solution 1: Prefix Sum + Enumerate Turning Point
62
+
63
+
Firstly, we need to understand that for a product, the number of trailing zeros depends on the smaller count of $2$ and $5$ in its factors. Also, each corner path should cover as many numbers as possible, so it must start from a boundary, reach a turning point, and then reach another boundary.
64
+
65
+
Therefore, we can create four two-dimensional arrays $r2$, $c2$, $r5$, $c5$ to record the counts of $2$ and $5$ in each row and column. Where:
66
+
67
+
-`r2[i][j]` represents the count of $2$ from the first column to the $j$-th column in the $i$-th row;
68
+
-`c2[i][j]` represents the count of $2$ from the first row to the $i$-th row in the $j$-th column;
69
+
-`r5[i][j]` represents the count of $5$ from the first column to the $j$-th column in the $i$-th row;
70
+
-`c5[i][j]` represents the count of $5$ from the first row to the $i$-th row in the $j$-th column.
71
+
72
+
Next, we traverse the two-dimensional array `grid`. For each number, we calculate its counts of $2$ and $5$, and then update the four two-dimensional arrays.
73
+
74
+
Then, we enumerate the turning point $(i, j)$. For each turning point, we calculate four values:
75
+
76
+
-`a` represents the smaller count of $2$ and $5$ in the path that moves right from $(i, 1)$ to $(i, j)$, then turns and moves up to $(1, j)$;
77
+
-`b` represents the smaller count of $2$ and $5$ in the path that moves right from $(i, 1)$ to $(i, j)$, then turns and moves down to $(m, j)$;
78
+
-`c` represents the smaller count of $2$ and $5$ in the path that moves left from $(i, n)$ to $(i, j)$, then turns and moves up to $(1, j)$;
79
+
-`d` represents the smaller count of $2$ and $5$ in the path that moves left from $(i, n)$ to $(i, j)$, then turns and moves down to $(m, j)$.
80
+
81
+
Each time we enumerate, we take the maximum of these four values, and then update the answer.
82
+
83
+
Finally, we return the answer.
84
+
85
+
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 of the `grid` array, respectively.
Copy file name to clipboardexpand all lines: solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md
+9-1
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,15 @@ It can be proven that there is no longer path that satisfies the conditions.
44
44
45
45
## Solutions
46
46
47
-
### Solution 1
47
+
### Solution 1: Tree-shaped DP
48
+
49
+
First, we construct an adjacency list $g$ based on the array $parent$, where $g[i]$ represents all child nodes of node $i$.
50
+
51
+
Then we start DFS from the root node. For each node $i$, we traverse each child node $j$ in $g[i]$. If $s[i] \neq s[j]$, then we can start from node $i$, pass through node $j$, and reach a leaf node. The length of this path is $x = 1 + \text{dfs}(j)$. We use $mx$ to record the longest path length starting from node $i$. At the same time, we update the answer $ans = \max(ans, mx + x)$ during the traversal process.
52
+
53
+
Finally, we return $ans + 1$.
54
+
55
+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes.
Copy file name to clipboardexpand all lines: solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md
+19-1
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,25 @@ Note that the trip 4 -> 1 -> 0 -> 1 is not allowed because you visit th
51
51
52
52
## Solutions
53
53
54
-
### Solution 1
54
+
### Solution 1: State Compression Dynamic Programming
55
+
56
+
We notice that the problem requires exactly $k$ roads to be passed, and each city can only be visited once. The number of cities is $n$, so we can pass at most $n - 1$ roads. Therefore, if $k \ge n$, we cannot meet the requirements of the problem, and we can directly return $-1$.
57
+
58
+
In addition, we can also find that the number of cities $n$ does not exceed $15$, which suggests that we can consider using the method of state compression dynamic programming to solve this problem. We use a binary number of length $n$ to represent the cities that have been passed, where the $i$-th bit is $1$ indicates that the $i$-th city has been passed, and $0$ indicates that the $i$-th city has not been passed yet.
59
+
60
+
We use $f[i][j]$ to represent the maximum travel cost when the cities that have been passed are $i$ and the last city passed is $j$. Initially, $f[2^i][i]=0$, and the rest $f[i][j]=-\infty$.
61
+
62
+
Consider how $f[i][j]$ transitions. For $f[i]$, we enumerate all cities $j$. If the $j$-th bit of $i$ is $1$, then we can reach city $j$ from other city $h$ through the road, at this time the value of $f[i][j]$ is the maximum value of $f[i][h]+cost(h, j)$, where $cost(h, j)$ represents the travel cost from city $h$ to city $j$. Therefore, we can get the state transition equation:
where $i \backslash j$ represents changing the $j$-th bit of $i$ to $0$.
69
+
70
+
After calculating $f[i][j]$, we judge whether the number of cities passed is $k+1$, that is, whether the number of $1$s in the binary representation of $i$ is $k+1$. If so, we update the answer as $ans = \max(ans, f[i][j])$.
71
+
72
+
The time complexity is $O(2^n \times n^2)$, and the space complexity is $O(2^n \times n)$, where $n$ represents the number of cities.
Copy file name to clipboardexpand all lines: solution/2200-2299/2248.Intersection of Multiple Arrays/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,11 @@ There does not exist any integer present both in nums[0] and nums[1], so we retu
38
38
39
39
## Solutions
40
40
41
-
### Solution 1
41
+
### Solution 1: Counting
42
+
43
+
Traverse the array `nums`. For each sub-array `arr`, count the occurrence of each number in `arr`. Then traverse the count array, count the numbers that appear as many times as the length of the array `nums`, which are the answers.
44
+
45
+
The time complexity is $O(N)$, and the space complexity is $O(1000)$. Where $N$ is the total number of numbers in the array `nums`.
0 commit comments