Skip to content

Commit 8621cd5

Browse files
authoredMay 11, 2024
feat: add solutions to lc problems: No.2244,2589 (#2788)
1 parent 0974ade commit 8621cd5

File tree

12 files changed

+215
-22
lines changed

12 files changed

+215
-22
lines changed
 

‎solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,28 @@ function minimumRounds(tasks: number[]): number {
142142
}
143143
```
144144

145+
```rust
146+
use std::collections::HashMap;
147+
148+
impl Solution {
149+
pub fn minimum_rounds(tasks: Vec<i32>) -> i32 {
150+
let mut cnt = HashMap::new();
151+
for &t in tasks.iter() {
152+
let count = cnt.entry(t).or_insert(0);
153+
*count += 1;
154+
}
155+
let mut ans = 0;
156+
for &v in cnt.values() {
157+
if v == 1 {
158+
return -1;
159+
}
160+
ans += v / 3 + (if v % 3 == 0 { 0 } else { 1 });
161+
}
162+
ans
163+
}
164+
}
165+
```
166+
145167
<!-- tabs:end -->
146168

147169
<!-- end -->

‎solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README_EN.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ It can be shown that all the tasks cannot be completed in fewer than 4 rounds, s
4242

4343
## Solutions
4444

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.
4652

4753
<!-- tabs:start -->
4854

@@ -134,6 +140,28 @@ function minimumRounds(tasks: number[]): number {
134140
}
135141
```
136142

143+
```rust
144+
use std::collections::HashMap;
145+
146+
impl Solution {
147+
pub fn minimum_rounds(tasks: Vec<i32>) -> i32 {
148+
let mut cnt = HashMap::new();
149+
for &t in tasks.iter() {
150+
let count = cnt.entry(t).or_insert(0);
151+
*count += 1;
152+
}
153+
let mut ans = 0;
154+
for &v in cnt.values() {
155+
if v == 1 {
156+
return -1;
157+
}
158+
ans += v / 3 + (if v % 3 == 0 { 0 } else { 1 });
159+
}
160+
ans
161+
}
162+
}
163+
```
164+
137165
<!-- tabs:end -->
138166

139167
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn minimum_rounds(tasks: Vec<i32>) -> i32 {
5+
let mut cnt = HashMap::new();
6+
for &t in tasks.iter() {
7+
let count = cnt.entry(t).or_insert(0);
8+
*count += 1;
9+
}
10+
let mut ans = 0;
11+
for &v in cnt.values() {
12+
if v == 1 {
13+
return -1;
14+
}
15+
ans += v / 3 + (if v % 3 == 0 { 0 } else { 1 });
16+
}
17+
ans
18+
}
19+
}

‎solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Soluttion.ts

-14
This file was deleted.

‎solution/2200-2299/2245.Maximum Trailing Zeros in a Cornered Path/README_EN.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,31 @@ There are no cornered paths in the grid that result in a product with a trailing
5858

5959
## Solutions
6060

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.
6286

6387
<!-- tabs:start -->
6488

‎solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ It can be proven that there is no longer path that satisfies the conditions.
4444

4545
## Solutions
4646

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.
4856

4957
<!-- tabs:start -->
5058

‎solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,25 @@ Note that the trip 4 -&gt; 1 -&gt; 0 -&gt; 1 is not allowed because you visit th
5151

5252
## Solutions
5353

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:
63+
64+
$$
65+
f[i][j]=\max_{h \in \text{city}}\{f[i \backslash j][h]+cost(h, j)\}
66+
$$
67+
68+
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.
5573

5674
<!-- tabs:start -->
5775

‎solution/2200-2299/2248.Intersection of Multiple Arrays/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ There does not exist any integer present both in nums[0] and nums[1], so we retu
3838

3939
## Solutions
4040

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`.
4246

4347
<!-- tabs:start -->
4448

‎solution/2500-2599/2589.Minimum Time to Complete All Tasks/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func findMinimumTime(tasks [][]int) (ans int) {
154154
```ts
155155
function findMinimumTime(tasks: number[][]): number {
156156
tasks.sort((a, b) => a[1] - b[1]);
157-
const vis = new Array(2010).fill(0);
157+
const vis: number[] = Array(2010).fill(0);
158158
let ans = 0;
159159
for (let [start, end, duration] of tasks) {
160160
for (let i = start; i <= end; ++i) {
@@ -171,6 +171,35 @@ function findMinimumTime(tasks: number[][]): number {
171171
}
172172
```
173173

174+
```rust
175+
impl Solution {
176+
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> i32 {
177+
let mut tasks = tasks;
178+
tasks.sort_by(|a, b| a[1].cmp(&b[1]));
179+
let mut vis = vec![0; 2010];
180+
let mut ans = 0;
181+
182+
for task in tasks {
183+
let start = task[0] as usize;
184+
let end = task[1] as usize;
185+
let mut duration = task[2] - vis[start..=end].iter().sum::<i32>();
186+
let mut i = end;
187+
188+
while i >= start && duration > 0 {
189+
if vis[i] == 0 {
190+
duration -= 1;
191+
vis[i] = 1;
192+
ans += 1;
193+
}
194+
i -= 1;
195+
}
196+
}
197+
198+
ans
199+
}
200+
}
201+
```
202+
174203
<!-- tabs:end -->
175204

176205
<!-- end -->

‎solution/2500-2599/2589.Minimum Time to Complete All Tasks/README_EN.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func findMinimumTime(tasks [][]int) (ans int) {
152152
```ts
153153
function findMinimumTime(tasks: number[][]): number {
154154
tasks.sort((a, b) => a[1] - b[1]);
155-
const vis = new Array(2010).fill(0);
155+
const vis: number[] = Array(2010).fill(0);
156156
let ans = 0;
157157
for (let [start, end, duration] of tasks) {
158158
for (let i = start; i <= end; ++i) {
@@ -169,6 +169,35 @@ function findMinimumTime(tasks: number[][]): number {
169169
}
170170
```
171171

172+
```rust
173+
impl Solution {
174+
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> i32 {
175+
let mut tasks = tasks;
176+
tasks.sort_by(|a, b| a[1].cmp(&b[1]));
177+
let mut vis = vec![0; 2010];
178+
let mut ans = 0;
179+
180+
for task in tasks {
181+
let start = task[0] as usize;
182+
let end = task[1] as usize;
183+
let mut duration = task[2] - vis[start..=end].iter().sum::<i32>();
184+
let mut i = end;
185+
186+
while i >= start && duration > 0 {
187+
if vis[i] == 0 {
188+
duration -= 1;
189+
vis[i] = 1;
190+
ans += 1;
191+
}
192+
i -= 1;
193+
}
194+
}
195+
196+
ans
197+
}
198+
}
199+
```
200+
172201
<!-- tabs:end -->
173202

174203
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> i32 {
3+
let mut tasks = tasks;
4+
tasks.sort_by(|a, b| a[1].cmp(&b[1]));
5+
let mut vis = vec![0; 2010];
6+
let mut ans = 0;
7+
8+
for task in tasks {
9+
let start = task[0] as usize;
10+
let end = task[1] as usize;
11+
let mut duration = task[2] - vis[start..=end].iter().sum::<i32>();
12+
let mut i = end;
13+
14+
while i >= start && duration > 0 {
15+
if vis[i] == 0 {
16+
duration -= 1;
17+
vis[i] = 1;
18+
ans += 1;
19+
}
20+
i -= 1;
21+
}
22+
}
23+
24+
ans
25+
}
26+
}

‎solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function findMinimumTime(tasks: number[][]): number {
22
tasks.sort((a, b) => a[1] - b[1]);
3-
const vis = new Array(2010).fill(0);
3+
const vis: number[] = Array(2010).fill(0);
44
let ans = 0;
55
for (let [start, end, duration] of tasks) {
66
for (let i = start; i <= end; ++i) {

0 commit comments

Comments
 (0)