Skip to content

Commit 9a701ae

Browse files
authored
feat: update solutions to lc problems: No.1883,3119 (#2618)
* No.1883.Minimum Skips to Arrive at Meeting On Time * No.3119.Maximum Number of Potholes That Can Be Fixed
1 parent 87d9b0b commit 9a701ae

File tree

11 files changed

+252
-61
lines changed

11 files changed

+252
-61
lines changed

solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md

+18-24
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ class Solution:
106106
return -1
107107
```
108108

109+
```python
110+
class Solution:
111+
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
112+
n = len(dist)
113+
f = [[inf] * (n + 1) for _ in range(n + 1)]
114+
f[0][0] = 0
115+
for i, x in enumerate(dist, 1):
116+
for j in range(i + 1):
117+
if j < i:
118+
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
119+
if j:
120+
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
121+
for j in range(n + 1):
122+
if f[n][j] <= hoursBefore * speed:
123+
return j
124+
return -1
125+
```
126+
109127
```java
110128
class Solution {
111129
public int minSkips(int[] dist, int speed, int hoursBefore) {
@@ -223,28 +241,4 @@ function minSkips(dist: number[], speed: number, hoursBefore: number): number {
223241

224242
<!-- tabs:end -->
225243

226-
### 方法二
227-
228-
<!-- tabs:start -->
229-
230-
```python
231-
class Solution:
232-
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
233-
n = len(dist)
234-
f = [[inf] * (n + 1) for _ in range(n + 1)]
235-
f[0][0] = 0
236-
for i, x in enumerate(dist, 1):
237-
for j in range(i + 1):
238-
if j < i:
239-
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
240-
if j:
241-
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
242-
for j in range(n + 1):
243-
if f[n][j] <= hoursBefore * speed:
244-
return j
245-
return -1
246-
```
247-
248-
<!-- tabs:end -->
249-
250244
<!-- end -->

solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md

+33-25
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,21 @@ You can skip the first and third rest to arrive in ((7/2 + <u>0</u>) + (3/2 + 0)
6565

6666
## Solutions
6767

68-
### Solution 1
68+
### Solution 1: Dynamic Programming
69+
70+
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$.
71+
72+
Since we can choose to skip or not skip the rest time of the $i$-th road, we can list the state transition equation:
73+
74+
$$
75+
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.
76+
$$
77+
78+
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.
79+
80+
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$.
81+
82+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$, where $n$ is the number of roads.
6983

7084
<!-- tabs:start -->
7185

@@ -88,6 +102,24 @@ class Solution:
88102
return -1
89103
```
90104

105+
```python
106+
class Solution:
107+
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
108+
n = len(dist)
109+
f = [[inf] * (n + 1) for _ in range(n + 1)]
110+
f[0][0] = 0
111+
for i, x in enumerate(dist, 1):
112+
for j in range(i + 1):
113+
if j < i:
114+
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
115+
if j:
116+
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
117+
for j in range(n + 1):
118+
if f[n][j] <= hoursBefore * speed:
119+
return j
120+
return -1
121+
```
122+
91123
```java
92124
class Solution {
93125
public int minSkips(int[] dist, int speed, int hoursBefore) {
@@ -205,28 +237,4 @@ function minSkips(dist: number[], speed: number, hoursBefore: number): number {
205237

206238
<!-- tabs:end -->
207239

208-
### Solution 2
209-
210-
<!-- tabs:start -->
211-
212-
```python
213-
class Solution:
214-
def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:
215-
n = len(dist)
216-
f = [[inf] * (n + 1) for _ in range(n + 1)]
217-
f[0][0] = 0
218-
for i, x in enumerate(dist, 1):
219-
for j in range(i + 1):
220-
if j < i:
221-
f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed)
222-
if j:
223-
f[i][j] = min(f[i][j], f[i - 1][j - 1] + x)
224-
for j in range(n + 1):
225-
if f[n][j] <= hoursBefore * speed:
226-
return j
227-
return -1
228-
```
229-
230-
<!-- tabs:end -->
231-
232240
<!-- end -->

solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README.md

+69-4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class Solution:
9494
t = min(budget // (k + 1), cnt[k])
9595
ans += t * k
9696
budget -= t * (k + 1)
97+
if budget == 0:
98+
break
9799
cnt[k - 1] += cnt[k] - t
98100
return ans
99101
```
@@ -114,7 +116,7 @@ class Solution {
114116
}
115117
}
116118
int ans = 0;
117-
for (k = n - 1; k > 0; --k) {
119+
for (k = n - 1; k > 0 && budget > 0; --k) {
118120
int t = Math.min(budget / (k + 1), cnt[k]);
119121
ans += t * k;
120122
budget -= t * (k + 1);
@@ -142,7 +144,7 @@ public:
142144
}
143145
}
144146
int ans = 0;
145-
for (k = n - 1; k; --k) {
147+
for (k = n - 1; k && budget; --k) {
146148
int t = min(budget / (k + 1), cnt[k]);
147149
ans += t * k;
148150
budget -= t * (k + 1);
@@ -167,7 +169,7 @@ func maxPotholes(road string, budget int) (ans int) {
167169
k = 0
168170
}
169171
}
170-
for k = n - 1; k > 0; k-- {
172+
for k = n - 1; k > 0 && budget > 0; k-- {
171173
t := min(budget/(k+1), cnt[k])
172174
ans += t * k
173175
budget -= t * (k + 1)
@@ -192,7 +194,7 @@ function maxPotholes(road: string, budget: number): number {
192194
}
193195
}
194196
let ans = 0;
195-
for (k = n - 1; k; --k) {
197+
for (k = n - 1; k && budget; --k) {
196198
const t = Math.min(Math.floor(budget / (k + 1)), cnt[k]);
197199
ans += t * k;
198200
budget -= t * (k + 1);
@@ -202,6 +204,69 @@ function maxPotholes(road: string, budget: number): number {
202204
}
203205
```
204206

207+
```rust
208+
impl Solution {
209+
pub fn max_potholes(road: String, budget: i32) -> i32 {
210+
let mut cs: Vec<char> = road.chars().collect();
211+
cs.push('.');
212+
let n = cs.len();
213+
let mut cnt: Vec<i32> = vec![0; n];
214+
let mut k = 0;
215+
216+
for c in cs.iter() {
217+
if *c == 'x' {
218+
k += 1;
219+
} else if k > 0 {
220+
cnt[k] += 1;
221+
k = 0;
222+
}
223+
}
224+
225+
let mut ans = 0;
226+
let mut budget = budget;
227+
228+
for k in (1..n).rev() {
229+
if budget == 0 {
230+
break;
231+
}
232+
let t = std::cmp::min(budget / ((k as i32) + 1), cnt[k]);
233+
ans += t * (k as i32);
234+
budget -= t * ((k as i32) + 1);
235+
cnt[k - 1] += cnt[k] - t;
236+
}
237+
238+
ans
239+
}
240+
}
241+
```
242+
243+
```cs
244+
public class Solution {
245+
public int MaxPotholes(string road, int budget) {
246+
road += '.';
247+
int n = road.Length;
248+
int[] cnt = new int[n];
249+
int k = 0;
250+
foreach (char c in road) {
251+
if (c == 'x') {
252+
++k;
253+
} else if (k > 0) {
254+
++cnt[k];
255+
k = 0;
256+
}
257+
}
258+
int ans = 0;
259+
for (k = n - 1; k > 0 && budget > 0; --k) {
260+
int t = Math.Min(budget / (k + 1), cnt[k]);
261+
ans += t * k;
262+
budget -= t * (k + 1);
263+
cnt[k - 1] += cnt[k] - t;
264+
}
265+
return ans;
266+
}
267+
}
268+
```
269+
205270
<!-- tabs:end -->
206271

207272
<!-- end -->

solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/README_EN.md

+69-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class Solution:
9292
t = min(budget // (k + 1), cnt[k])
9393
ans += t * k
9494
budget -= t * (k + 1)
95+
if budget == 0:
96+
break
9597
cnt[k - 1] += cnt[k] - t
9698
return ans
9799
```
@@ -112,7 +114,7 @@ class Solution {
112114
}
113115
}
114116
int ans = 0;
115-
for (k = n - 1; k > 0; --k) {
117+
for (k = n - 1; k > 0 && budget > 0; --k) {
116118
int t = Math.min(budget / (k + 1), cnt[k]);
117119
ans += t * k;
118120
budget -= t * (k + 1);
@@ -140,7 +142,7 @@ public:
140142
}
141143
}
142144
int ans = 0;
143-
for (k = n - 1; k; --k) {
145+
for (k = n - 1; k && budget; --k) {
144146
int t = min(budget / (k + 1), cnt[k]);
145147
ans += t * k;
146148
budget -= t * (k + 1);
@@ -165,7 +167,7 @@ func maxPotholes(road string, budget int) (ans int) {
165167
k = 0
166168
}
167169
}
168-
for k = n - 1; k > 0; k-- {
170+
for k = n - 1; k > 0 && budget > 0; k-- {
169171
t := min(budget/(k+1), cnt[k])
170172
ans += t * k
171173
budget -= t * (k + 1)
@@ -190,7 +192,7 @@ function maxPotholes(road: string, budget: number): number {
190192
}
191193
}
192194
let ans = 0;
193-
for (k = n - 1; k; --k) {
195+
for (k = n - 1; k && budget; --k) {
194196
const t = Math.min(Math.floor(budget / (k + 1)), cnt[k]);
195197
ans += t * k;
196198
budget -= t * (k + 1);
@@ -200,6 +202,69 @@ function maxPotholes(road: string, budget: number): number {
200202
}
201203
```
202204

205+
```rust
206+
impl Solution {
207+
pub fn max_potholes(road: String, budget: i32) -> i32 {
208+
let mut cs: Vec<char> = road.chars().collect();
209+
cs.push('.');
210+
let n = cs.len();
211+
let mut cnt: Vec<i32> = vec![0; n];
212+
let mut k = 0;
213+
214+
for c in cs.iter() {
215+
if *c == 'x' {
216+
k += 1;
217+
} else if k > 0 {
218+
cnt[k] += 1;
219+
k = 0;
220+
}
221+
}
222+
223+
let mut ans = 0;
224+
let mut budget = budget;
225+
226+
for k in (1..n).rev() {
227+
if budget == 0 {
228+
break;
229+
}
230+
let t = std::cmp::min(budget / ((k as i32) + 1), cnt[k]);
231+
ans += t * (k as i32);
232+
budget -= t * ((k as i32) + 1);
233+
cnt[k - 1] += cnt[k] - t;
234+
}
235+
236+
ans
237+
}
238+
}
239+
```
240+
241+
```cs
242+
public class Solution {
243+
public int MaxPotholes(string road, int budget) {
244+
road += '.';
245+
int n = road.Length;
246+
int[] cnt = new int[n];
247+
int k = 0;
248+
foreach (char c in road) {
249+
if (c == 'x') {
250+
++k;
251+
} else if (k > 0) {
252+
++cnt[k];
253+
k = 0;
254+
}
255+
}
256+
int ans = 0;
257+
for (k = n - 1; k > 0 && budget > 0; --k) {
258+
int t = Math.Min(budget / (k + 1), cnt[k]);
259+
ans += t * k;
260+
budget -= t * (k + 1);
261+
cnt[k - 1] += cnt[k] - t;
262+
}
263+
return ans;
264+
}
265+
}
266+
```
267+
203268
<!-- tabs:end -->
204269

205270
<!-- end -->

solution/3100-3199/3119.Maximum Number of Potholes That Can Be Fixed/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Solution {
1414
}
1515
}
1616
int ans = 0;
17-
for (k = n - 1; k; --k) {
17+
for (k = n - 1; k && budget; --k) {
1818
int t = min(budget / (k + 1), cnt[k]);
1919
ans += t * k;
2020
budget -= t * (k + 1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public int MaxPotholes(string road, int budget) {
3+
road += '.';
4+
int n = road.Length;
5+
int[] cnt = new int[n];
6+
int k = 0;
7+
foreach (char c in road) {
8+
if (c == 'x') {
9+
++k;
10+
} else if (k > 0) {
11+
++cnt[k];
12+
k = 0;
13+
}
14+
}
15+
int ans = 0;
16+
for (k = n - 1; k > 0 && budget > 0; --k) {
17+
int t = Math.Min(budget / (k + 1), cnt[k]);
18+
ans += t * k;
19+
budget -= t * (k + 1);
20+
cnt[k - 1] += cnt[k] - t;
21+
}
22+
return ans;
23+
}
24+
}

0 commit comments

Comments
 (0)