diff --git a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README.md b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README.md index b166775085d79..1714cd71411fe 100644 --- a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README.md +++ b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README.md @@ -142,6 +142,28 @@ function minimumRounds(tasks: number[]): number { } ``` +```rust +use std::collections::HashMap; + +impl Solution { + pub fn minimum_rounds(tasks: Vec) -> i32 { + let mut cnt = HashMap::new(); + for &t in tasks.iter() { + let count = cnt.entry(t).or_insert(0); + *count += 1; + } + let mut ans = 0; + for &v in cnt.values() { + if v == 1 { + return -1; + } + ans += v / 3 + (if v % 3 == 0 { 0 } else { 1 }); + } + ans + } +} +``` + diff --git a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README_EN.md b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README_EN.md index 0db0f37af67e2..07e72583d1e58 100644 --- a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README_EN.md +++ b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/README_EN.md @@ -42,7 +42,13 @@ It can be shown that all the tasks cannot be completed in fewer than 4 rounds, s ## Solutions -### Solution 1 +### Solution 1: Hash Table + +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. + +Finally, we return the answer. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the `tasks` array. @@ -134,6 +140,28 @@ function minimumRounds(tasks: number[]): number { } ``` +```rust +use std::collections::HashMap; + +impl Solution { + pub fn minimum_rounds(tasks: Vec) -> i32 { + let mut cnt = HashMap::new(); + for &t in tasks.iter() { + let count = cnt.entry(t).or_insert(0); + *count += 1; + } + let mut ans = 0; + for &v in cnt.values() { + if v == 1 { + return -1; + } + ans += v / 3 + (if v % 3 == 0 { 0 } else { 1 }); + } + ans + } +} +``` + diff --git a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Solution.rs b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Solution.rs new file mode 100644 index 0000000000000..20d3bb6b549a9 --- /dev/null +++ b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Solution.rs @@ -0,0 +1,19 @@ +use std::collections::HashMap; + +impl Solution { + pub fn minimum_rounds(tasks: Vec) -> i32 { + let mut cnt = HashMap::new(); + for &t in tasks.iter() { + let count = cnt.entry(t).or_insert(0); + *count += 1; + } + let mut ans = 0; + for &v in cnt.values() { + if v == 1 { + return -1; + } + ans += v / 3 + (if v % 3 == 0 { 0 } else { 1 }); + } + ans + } +} diff --git a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Soluttion.ts b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Soluttion.ts deleted file mode 100644 index 8830dc79b9cc0..0000000000000 --- a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Soluttion.ts +++ /dev/null @@ -1,14 +0,0 @@ -function minimumRounds(tasks: number[]): number { - const cnt = new Map(); - for (const t of tasks) { - cnt.set(t, (cnt.get(t) || 0) + 1); - } - let ans = 0; - for (const v of cnt.values()) { - if (v == 1) { - return -1; - } - ans += Math.floor(v / 3) + (v % 3 === 0 ? 0 : 1); - } - return ans; -} diff --git a/solution/2200-2299/2245.Maximum Trailing Zeros in a Cornered Path/README_EN.md b/solution/2200-2299/2245.Maximum Trailing Zeros in a Cornered Path/README_EN.md index a7e518e565ab3..eafc4d01f14d5 100644 --- a/solution/2200-2299/2245.Maximum Trailing Zeros in a Cornered Path/README_EN.md +++ b/solution/2200-2299/2245.Maximum Trailing Zeros in a Cornered Path/README_EN.md @@ -58,7 +58,31 @@ There are no cornered paths in the grid that result in a product with a trailing ## Solutions -### Solution 1 +### Solution 1: Prefix Sum + Enumerate Turning Point + +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. + +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: + +- `r2[i][j]` represents the count of $2$ from the first column to the $j$-th column in the $i$-th row; +- `c2[i][j]` represents the count of $2$ from the first row to the $i$-th row in the $j$-th column; +- `r5[i][j]` represents the count of $5$ from the first column to the $j$-th column in the $i$-th row; +- `c5[i][j]` represents the count of $5$ from the first row to the $i$-th row in the $j$-th column. + +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. + +Then, we enumerate the turning point $(i, j)$. For each turning point, we calculate four values: + +- `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)$; +- `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)$; +- `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)$; +- `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)$. + +Each time we enumerate, we take the maximum of these four values, and then update the answer. + +Finally, we return the answer. + +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. diff --git a/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md b/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md index dd96aa4f2b86d..2dee010b68de6 100644 --- a/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md +++ b/solution/2200-2299/2246.Longest Path With Different Adjacent Characters/README_EN.md @@ -44,7 +44,15 @@ It can be proven that there is no longer path that satisfies the conditions. ## Solutions -### Solution 1 +### Solution 1: Tree-shaped DP + +First, we construct an adjacency list $g$ based on the array $parent$, where $g[i]$ represents all child nodes of node $i$. + +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. + +Finally, we return $ans + 1$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes. diff --git a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md index 9e9de8e979ebc..21601a72d711d 100644 --- a/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md +++ b/solution/2200-2299/2247.Maximum Cost of Trip With K Highways/README_EN.md @@ -51,7 +51,25 @@ Note that the trip 4 -> 1 -> 0 -> 1 is not allowed because you visit th ## Solutions -### Solution 1 +### Solution 1: State Compression Dynamic Programming + +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$. + +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. + +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$. + +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: + +$$ +f[i][j]=\max_{h \in \text{city}}\{f[i \backslash j][h]+cost(h, j)\} +$$ + +where $i \backslash j$ represents changing the $j$-th bit of $i$ to $0$. + +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])$. + +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. diff --git a/solution/2200-2299/2248.Intersection of Multiple Arrays/README_EN.md b/solution/2200-2299/2248.Intersection of Multiple Arrays/README_EN.md index f7c13541ee630..5b9214345eff9 100644 --- a/solution/2200-2299/2248.Intersection of Multiple Arrays/README_EN.md +++ b/solution/2200-2299/2248.Intersection of Multiple Arrays/README_EN.md @@ -38,7 +38,11 @@ There does not exist any integer present both in nums[0] and nums[1], so we retu ## Solutions -### Solution 1 +### Solution 1: Counting + +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. + +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`. diff --git a/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README.md b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README.md index abcefc7f29284..87b9bcbb9477b 100644 --- a/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README.md +++ b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README.md @@ -154,7 +154,7 @@ func findMinimumTime(tasks [][]int) (ans int) { ```ts function findMinimumTime(tasks: number[][]): number { tasks.sort((a, b) => a[1] - b[1]); - const vis = new Array(2010).fill(0); + const vis: number[] = Array(2010).fill(0); let ans = 0; for (let [start, end, duration] of tasks) { for (let i = start; i <= end; ++i) { @@ -171,6 +171,35 @@ function findMinimumTime(tasks: number[][]): number { } ``` +```rust +impl Solution { + pub fn find_minimum_time(tasks: Vec>) -> i32 { + let mut tasks = tasks; + tasks.sort_by(|a, b| a[1].cmp(&b[1])); + let mut vis = vec![0; 2010]; + let mut ans = 0; + + for task in tasks { + let start = task[0] as usize; + let end = task[1] as usize; + let mut duration = task[2] - vis[start..=end].iter().sum::(); + let mut i = end; + + while i >= start && duration > 0 { + if vis[i] == 0 { + duration -= 1; + vis[i] = 1; + ans += 1; + } + i -= 1; + } + } + + ans + } +} +``` + diff --git a/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README_EN.md b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README_EN.md index 36eb8c8df43f1..396eafc036d6e 100644 --- a/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README_EN.md +++ b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/README_EN.md @@ -152,7 +152,7 @@ func findMinimumTime(tasks [][]int) (ans int) { ```ts function findMinimumTime(tasks: number[][]): number { tasks.sort((a, b) => a[1] - b[1]); - const vis = new Array(2010).fill(0); + const vis: number[] = Array(2010).fill(0); let ans = 0; for (let [start, end, duration] of tasks) { for (let i = start; i <= end; ++i) { @@ -169,6 +169,35 @@ function findMinimumTime(tasks: number[][]): number { } ``` +```rust +impl Solution { + pub fn find_minimum_time(tasks: Vec>) -> i32 { + let mut tasks = tasks; + tasks.sort_by(|a, b| a[1].cmp(&b[1])); + let mut vis = vec![0; 2010]; + let mut ans = 0; + + for task in tasks { + let start = task[0] as usize; + let end = task[1] as usize; + let mut duration = task[2] - vis[start..=end].iter().sum::(); + let mut i = end; + + while i >= start && duration > 0 { + if vis[i] == 0 { + duration -= 1; + vis[i] = 1; + ans += 1; + } + i -= 1; + } + } + + ans + } +} +``` + diff --git a/solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.rs b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.rs new file mode 100644 index 0000000000000..fe0947d10e077 --- /dev/null +++ b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.rs @@ -0,0 +1,26 @@ +impl Solution { + pub fn find_minimum_time(tasks: Vec>) -> i32 { + let mut tasks = tasks; + tasks.sort_by(|a, b| a[1].cmp(&b[1])); + let mut vis = vec![0; 2010]; + let mut ans = 0; + + for task in tasks { + let start = task[0] as usize; + let end = task[1] as usize; + let mut duration = task[2] - vis[start..=end].iter().sum::(); + let mut i = end; + + while i >= start && duration > 0 { + if vis[i] == 0 { + duration -= 1; + vis[i] = 1; + ans += 1; + } + i -= 1; + } + } + + ans + } +} diff --git a/solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.ts b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.ts index 7bb54394b31a9..49626249f2d47 100644 --- a/solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.ts +++ b/solution/2500-2599/2589.Minimum Time to Complete All Tasks/Solution.ts @@ -1,6 +1,6 @@ function findMinimumTime(tasks: number[][]): number { tasks.sort((a, b) => a[1] - b[1]); - const vis = new Array(2010).fill(0); + const vis: number[] = Array(2010).fill(0); let ans = 0; for (let [start, end, duration] of tasks) { for (let i = start; i <= end; ++i) {