Skip to content
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

feat: update solutions to lc problems: No.1883,3119 #2618

Merged
merged 1 commit into from
Apr 19, 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 @@ -106,6 +106,24 @@ class Solution:
return -1
```

```python
class Solution:
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
n = len(dist)
f = [[inf] * (n + 1) for _ in range(n + 1)]
f[0][0] = 0
for i, x in enumerate(dist, 1):
for j in range(i + 1):
if j < i:
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
if j:
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
for j in range(n + 1):
if f[n][j] <= hoursBefore * speed:
return j
return -1
```

```java
class Solution {
public int minSkips(int[] dist, int speed, int hoursBefore) {
Expand Down Expand Up @@ -223,28 +241,4 @@ function minSkips(dist: number[], speed: number, hoursBefore: number): number {

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```python
class Solution:
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
n = len(dist)
f = [[inf] * (n + 1) for _ in range(n + 1)]
f[0][0] = 0
for i, x in enumerate(dist, 1):
for j in range(i + 1):
if j < i:
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
if j:
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
for j in range(n + 1):
if f[n][j] <= hoursBefore * speed:
return j
return -1
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,21 @@ You can skip the first and third rest to arrive in ((7/2 + <u>0</u>) + (3/2 + 0)

## Solutions

### Solution 1
### Solution 1: Dynamic Programming

We define $f[i][j]$ as the shortest time considering the first $i$ roads and exactly skipping $j$ rest times. Initially, $f[0][0]=0$, and the rest $f[i][j]=\infty$.

Since we can choose to skip or not skip the rest time of the $i$-th road, we can list the state transition equation:

$$
f[i][j]=\min\left\{\begin{aligned} \lceil f[i-1][j]+\frac{d_i}{s}\rceil & \text{Do not skip the rest time of the $i$-th road} \\ f[i-1][j-1]+\frac{d_i}{s} & \text{Skip the rest time of the $i$-th road} \end{aligned}\right.
$$

Where $\lceil x\rceil$ represents rounding $x$ up. It should be noted that since we need to ensure that exactly $j$ rest times are skipped, we must have $j\le i$; moreover, if $j=0$, no rest time can be skipped.

Due to the possible precision error brought by floating-point operations and rounding up, we introduce a constant $eps = 10^{-8}$ to represent a very small positive real number. We subtract $eps$ before rounding the floating-point number, and finally, when comparing $f[n][j]$ and $hoursBefore$, we need to add $eps$.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$, where $n$ is the number of roads.

<!-- tabs:start -->

Expand All @@ -88,6 +102,24 @@ class Solution:
return -1
```

```python
class Solution:
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
n = len(dist)
f = [[inf] * (n + 1) for _ in range(n + 1)]
f[0][0] = 0
for i, x in enumerate(dist, 1):
for j in range(i + 1):
if j < i:
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
if j:
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
for j in range(n + 1):
if f[n][j] <= hoursBefore * speed:
return j
return -1
```

```java
class Solution {
public int minSkips(int[] dist, int speed, int hoursBefore) {
Expand Down Expand Up @@ -205,28 +237,4 @@ function minSkips(dist: number[], speed: number, hoursBefore: number): number {

<!-- tabs:end -->

### Solution 2

<!-- tabs:start -->

```python
class Solution:
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
n = len(dist)
f = [[inf] * (n + 1) for _ in range(n + 1)]
f[0][0] = 0
for i, x in enumerate(dist, 1):
for j in range(i + 1):
if j < i:
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
if j:
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
for j in range(n + 1):
if f[n][j] <= hoursBefore * speed:
return j
return -1
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class Solution:
t = min(budget // (k + 1), cnt[k])
ans += t * k
budget -= t * (k + 1)
if budget == 0:
break
cnt[k - 1] += cnt[k] - t
return ans
```
Expand All @@ -114,7 +116,7 @@ class Solution {
}
}
int ans = 0;
for (k = n - 1; k > 0; --k) {
for (k = n - 1; k > 0 && budget > 0; --k) {
int t = Math.min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
Expand Down Expand Up @@ -142,7 +144,7 @@ public:
}
}
int ans = 0;
for (k = n - 1; k; --k) {
for (k = n - 1; k && budget; --k) {
int t = min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
Expand All @@ -167,7 +169,7 @@ func maxPotholes(road string, budget int) (ans int) {
k = 0
}
}
for k = n - 1; k > 0; k-- {
for k = n - 1; k > 0 && budget > 0; k-- {
t := min(budget/(k+1), cnt[k])
ans += t * k
budget -= t * (k + 1)
Expand All @@ -192,7 +194,7 @@ function maxPotholes(road: string, budget: number): number {
}
}
let ans = 0;
for (k = n - 1; k; --k) {
for (k = n - 1; k && budget; --k) {
const t = Math.min(Math.floor(budget / (k + 1)), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
Expand All @@ -202,6 +204,69 @@ function maxPotholes(road: string, budget: number): number {
}
```

```rust
impl Solution {
pub fn max_potholes(road: String, budget: i32) -> i32 {
let mut cs: Vec<char> = road.chars().collect();
cs.push('.');
let n = cs.len();
let mut cnt: Vec<i32> = vec![0; n];
let mut k = 0;

for c in cs.iter() {
if *c == 'x' {
k += 1;
} else if k > 0 {
cnt[k] += 1;
k = 0;
}
}

let mut ans = 0;
let mut budget = budget;

for k in (1..n).rev() {
if budget == 0 {
break;
}
let t = std::cmp::min(budget / ((k as i32) + 1), cnt[k]);
ans += t * (k as i32);
budget -= t * ((k as i32) + 1);
cnt[k - 1] += cnt[k] - t;
}

ans
}
}
```

```cs
public class Solution {
public int MaxPotholes(string road, int budget) {
road += '.';
int n = road.Length;
int[] cnt = new int[n];
int k = 0;
foreach (char c in road) {
if (c == 'x') {
++k;
} else if (k > 0) {
++cnt[k];
k = 0;
}
}
int ans = 0;
for (k = n - 1; k > 0 && budget > 0; --k) {
int t = Math.Min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
cnt[k - 1] += cnt[k] - t;
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class Solution:
t = min(budget // (k + 1), cnt[k])
ans += t * k
budget -= t * (k + 1)
if budget == 0:
break
cnt[k - 1] += cnt[k] - t
return ans
```
Expand All @@ -112,7 +114,7 @@ class Solution {
}
}
int ans = 0;
for (k = n - 1; k > 0; --k) {
for (k = n - 1; k > 0 && budget > 0; --k) {
int t = Math.min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
Expand Down Expand Up @@ -140,7 +142,7 @@ public:
}
}
int ans = 0;
for (k = n - 1; k; --k) {
for (k = n - 1; k && budget; --k) {
int t = min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
Expand All @@ -165,7 +167,7 @@ func maxPotholes(road string, budget int) (ans int) {
k = 0
}
}
for k = n - 1; k > 0; k-- {
for k = n - 1; k > 0 && budget > 0; k-- {
t := min(budget/(k+1), cnt[k])
ans += t * k
budget -= t * (k + 1)
Expand All @@ -190,7 +192,7 @@ function maxPotholes(road: string, budget: number): number {
}
}
let ans = 0;
for (k = n - 1; k; --k) {
for (k = n - 1; k && budget; --k) {
const t = Math.min(Math.floor(budget / (k + 1)), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
Expand All @@ -200,6 +202,69 @@ function maxPotholes(road: string, budget: number): number {
}
```

```rust
impl Solution {
pub fn max_potholes(road: String, budget: i32) -> i32 {
let mut cs: Vec<char> = road.chars().collect();
cs.push('.');
let n = cs.len();
let mut cnt: Vec<i32> = vec![0; n];
let mut k = 0;

for c in cs.iter() {
if *c == 'x' {
k += 1;
} else if k > 0 {
cnt[k] += 1;
k = 0;
}
}

let mut ans = 0;
let mut budget = budget;

for k in (1..n).rev() {
if budget == 0 {
break;
}
let t = std::cmp::min(budget / ((k as i32) + 1), cnt[k]);
ans += t * (k as i32);
budget -= t * ((k as i32) + 1);
cnt[k - 1] += cnt[k] - t;
}

ans
}
}
```

```cs
public class Solution {
public int MaxPotholes(string road, int budget) {
road += '.';
int n = road.Length;
int[] cnt = new int[n];
int k = 0;
foreach (char c in road) {
if (c == 'x') {
++k;
} else if (k > 0) {
++cnt[k];
k = 0;
}
}
int ans = 0;
for (k = n - 1; k > 0 && budget > 0; --k) {
int t = Math.Min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
cnt[k - 1] += cnt[k] - t;
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Solution {
}
}
int ans = 0;
for (k = n - 1; k; --k) {
for (k = n - 1; k && budget; --k) {
int t = min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
public int MaxPotholes(string road, int budget) {
road += '.';
int n = road.Length;
int[] cnt = new int[n];
int k = 0;
foreach (char c in road) {
if (c == 'x') {
++k;
} else if (k > 0) {
++cnt[k];
k = 0;
}
}
int ans = 0;
for (k = n - 1; k > 0 && budget > 0; --k) {
int t = Math.Min(budget / (k + 1), cnt[k]);
ans += t * k;
budget -= t * (k + 1);
cnt[k - 1] += cnt[k] - t;
}
return ans;
}
}
Loading
Loading