Skip to content

Commit 5158b03

Browse files
authored
feat: add solutions to lc problem: No.0568 (#4274)
1 parent c6e0817 commit 5158b03

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

solution/0500-0599/0568.Maximum Vacation Days/README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Ans = 7 + 7 + 7 = 21
9898

9999
我们定义 $f[k][j]$ 表示前 $k$ 周,且最后一周在城市 $j$ 休假的最长天数。初始时 $f[0][0]=0$,其它 $f[0][j]=-\infty$。答案为 $\max_{j=0}^{n-1} f[K][j]$。
100100

101-
接下来,我们考虑如何计算 $f[k][j]$。对于当前这一周,我们可以枚举上一周所在的城市 $i$,城市 $i$ 可以和城市 $j$ 相等,那么 $f[k][j] = f[k-1][i]$;也可以和城市 $j$ 不相等,如果不相等,我们需要判断是否可以从城市 $i$ 飞到城市 $j$,如果可以,那么 $f[k][j] = max(f[k][j], f[k-1][i])$。最后,我们还需要加上这一周在城市 $j$ 休假的天数 $days[j][k-1]$。
101+
接下来,我们考虑如何计算 $f[k][j]$。对于当前这一周,我们可以枚举上一周所在的城市 $i$,城市 $i$ 可以和城市 $j$ 相等,那么 $f[k][j] = f[k-1][i]$;也可以和城市 $j$ 不相等,如果不相等,我们需要判断是否可以从城市 $i$ 飞到城市 $j$,如果可以,那么 $f[k][j] = \max(f[k][j], f[k-1][i])$。最后,我们还需要加上这一周在城市 $j$ 休假的天数 $\textit{days}[j][k-1]$。
102102

103103
最终的答案即为 $\max_{j=0}^{n-1} f[K][j]$。
104104

@@ -220,6 +220,59 @@ func maxVacationDays(flights [][]int, days [][]int) (ans int) {
220220
}
221221
```
222222

223+
#### TypeScript
224+
225+
```ts
226+
function maxVacationDays(flights: number[][], days: number[][]): number {
227+
const n = flights.length;
228+
const K = days[0].length;
229+
const inf = Number.NEGATIVE_INFINITY;
230+
const f: number[][] = Array.from({ length: K + 1 }, () => Array(n).fill(inf));
231+
f[0][0] = 0;
232+
for (let k = 1; k <= K; k++) {
233+
for (let j = 0; j < n; j++) {
234+
f[k][j] = f[k - 1][j];
235+
for (let i = 0; i < n; i++) {
236+
if (flights[i][j]) {
237+
f[k][j] = Math.max(f[k][j], f[k - 1][i]);
238+
}
239+
}
240+
f[k][j] += days[j][k - 1];
241+
}
242+
}
243+
return Math.max(...f[K]);
244+
}
245+
```
246+
247+
#### Rust
248+
249+
```rust
250+
impl Solution {
251+
pub fn max_vacation_days(flights: Vec<Vec<i32>>, days: Vec<Vec<i32>>) -> i32 {
252+
let n = flights.len();
253+
let k = days[0].len();
254+
let inf = i32::MIN;
255+
256+
let mut f = vec![vec![inf; n]; k + 1];
257+
f[0][0] = 0;
258+
259+
for step in 1..=k {
260+
for j in 0..n {
261+
f[step][j] = f[step - 1][j];
262+
for i in 0..n {
263+
if flights[i][j] == 1 {
264+
f[step][j] = f[step][j].max(f[step - 1][i]);
265+
}
266+
}
267+
f[step][j] += days[j][step - 1];
268+
}
269+
}
270+
271+
*f[k].iter().max().unwrap()
272+
}
273+
}
274+
```
275+
223276
<!-- tabs:end -->
224277

225278
<!-- solution:end -->

solution/0500-0599/0568.Maximum Vacation Days/README_EN.md

+53
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,59 @@ func maxVacationDays(flights [][]int, days [][]int) (ans int) {
211211
}
212212
```
213213

214+
#### TypeScript
215+
216+
```ts
217+
function maxVacationDays(flights: number[][], days: number[][]): number {
218+
const n = flights.length;
219+
const K = days[0].length;
220+
const inf = Number.NEGATIVE_INFINITY;
221+
const f: number[][] = Array.from({ length: K + 1 }, () => Array(n).fill(inf));
222+
f[0][0] = 0;
223+
for (let k = 1; k <= K; k++) {
224+
for (let j = 0; j < n; j++) {
225+
f[k][j] = f[k - 1][j];
226+
for (let i = 0; i < n; i++) {
227+
if (flights[i][j]) {
228+
f[k][j] = Math.max(f[k][j], f[k - 1][i]);
229+
}
230+
}
231+
f[k][j] += days[j][k - 1];
232+
}
233+
}
234+
return Math.max(...f[K]);
235+
}
236+
```
237+
238+
#### Rust
239+
240+
```rust
241+
impl Solution {
242+
pub fn max_vacation_days(flights: Vec<Vec<i32>>, days: Vec<Vec<i32>>) -> i32 {
243+
let n = flights.len();
244+
let k = days[0].len();
245+
let inf = i32::MIN;
246+
247+
let mut f = vec![vec![inf; n]; k + 1];
248+
f[0][0] = 0;
249+
250+
for step in 1..=k {
251+
for j in 0..n {
252+
f[step][j] = f[step - 1][j];
253+
for i in 0..n {
254+
if flights[i][j] == 1 {
255+
f[step][j] = f[step][j].max(f[step - 1][i]);
256+
}
257+
}
258+
f[step][j] += days[j][step - 1];
259+
}
260+
}
261+
262+
*f[k].iter().max().unwrap()
263+
}
264+
}
265+
```
266+
214267
<!-- tabs:end -->
215268

216269
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn max_vacation_days(flights: Vec<Vec<i32>>, days: Vec<Vec<i32>>) -> i32 {
3+
let n = flights.len();
4+
let k = days[0].len();
5+
let inf = i32::MIN;
6+
7+
let mut f = vec![vec![inf; n]; k + 1];
8+
f[0][0] = 0;
9+
10+
for step in 1..=k {
11+
for j in 0..n {
12+
f[step][j] = f[step - 1][j];
13+
for i in 0..n {
14+
if flights[i][j] == 1 {
15+
f[step][j] = f[step][j].max(f[step - 1][i]);
16+
}
17+
}
18+
f[step][j] += days[j][step - 1];
19+
}
20+
}
21+
22+
*f[k].iter().max().unwrap()
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function maxVacationDays(flights: number[][], days: number[][]): number {
2+
const n = flights.length;
3+
const K = days[0].length;
4+
const inf = Number.NEGATIVE_INFINITY;
5+
const f: number[][] = Array.from({ length: K + 1 }, () => Array(n).fill(inf));
6+
f[0][0] = 0;
7+
for (let k = 1; k <= K; k++) {
8+
for (let j = 0; j < n; j++) {
9+
f[k][j] = f[k - 1][j];
10+
for (let i = 0; i < n; i++) {
11+
if (flights[i][j]) {
12+
f[k][j] = Math.max(f[k][j], f[k - 1][i]);
13+
}
14+
}
15+
f[k][j] += days[j][k - 1];
16+
}
17+
}
18+
return Math.max(...f[K]);
19+
}

0 commit comments

Comments
 (0)