Skip to content

feat: add solutions to lc problems: No.2244,2589 #2788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ function minimumRounds(tasks: number[]): number {
}
```

```rust
use std::collections::HashMap;

impl Solution {
pub fn minimum_rounds(tasks: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand Down Expand Up @@ -134,6 +140,28 @@ function minimumRounds(tasks: number[]): number {
}
```

```rust
use std::collections::HashMap;

impl Solution {
pub fn minimum_rounds(tasks: Vec<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::collections::HashMap;

impl Solution {
pub fn minimum_rounds(tasks: Vec<i32>) -> 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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,25 @@ Note that the trip 4 -&gt; 1 -&gt; 0 -&gt; 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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -171,6 +171,35 @@ function findMinimumTime(tasks: number[][]): number {
}
```

```rust
impl Solution {
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> 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::<i32>();
let mut i = end;

while i >= start && duration > 0 {
if vis[i] == 0 {
duration -= 1;
vis[i] = 1;
ans += 1;
}
i -= 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -169,6 +169,35 @@ function findMinimumTime(tasks: number[][]): number {
}
```

```rust
impl Solution {
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> 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::<i32>();
let mut i = end;

while i >= start && duration > 0 {
if vis[i] == 0 {
duration -= 1;
vis[i] = 1;
ans += 1;
}
i -= 1;
}
}

ans
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
impl Solution {
pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> 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::<i32>();
let mut i = end;

while i >= start && duration > 0 {
if vis[i] == 0 {
duration -= 1;
vis[i] = 1;
ans += 1;
}
i -= 1;
}
}

ans
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down