Skip to content
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
55 changes: 54 additions & 1 deletion solution/0500-0599/0568.Maximum Vacation Days/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Ans = 7 + 7 + 7 = 21

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

接下来,我们考虑如何计算 $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]$。
接下来,我们考虑如何计算 $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]$。

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

Expand Down Expand Up @@ -220,6 +220,59 @@ func maxVacationDays(flights [][]int, days [][]int) (ans int) {
}
```

#### TypeScript

```ts
function maxVacationDays(flights: number[][], days: number[][]): number {
const n = flights.length;
const K = days[0].length;
const inf = Number.NEGATIVE_INFINITY;
const f: number[][] = Array.from({ length: K + 1 }, () => Array(n).fill(inf));
f[0][0] = 0;
for (let k = 1; k <= K; k++) {
for (let j = 0; j < n; j++) {
f[k][j] = f[k - 1][j];
for (let i = 0; i < n; i++) {
if (flights[i][j]) {
f[k][j] = Math.max(f[k][j], f[k - 1][i]);
}
}
f[k][j] += days[j][k - 1];
}
}
return Math.max(...f[K]);
}
```

#### Rust

```rust
impl Solution {
pub fn max_vacation_days(flights: Vec<Vec<i32>>, days: Vec<Vec<i32>>) -> i32 {
let n = flights.len();
let k = days[0].len();
let inf = i32::MIN;

let mut f = vec![vec![inf; n]; k + 1];
f[0][0] = 0;

for step in 1..=k {
for j in 0..n {
f[step][j] = f[step - 1][j];
for i in 0..n {
if flights[i][j] == 1 {
f[step][j] = f[step][j].max(f[step - 1][i]);
}
}
f[step][j] += days[j][step - 1];
}
}

*f[k].iter().max().unwrap()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
53 changes: 53 additions & 0 deletions solution/0500-0599/0568.Maximum Vacation Days/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,59 @@ func maxVacationDays(flights [][]int, days [][]int) (ans int) {
}
```

#### TypeScript

```ts
function maxVacationDays(flights: number[][], days: number[][]): number {
const n = flights.length;
const K = days[0].length;
const inf = Number.NEGATIVE_INFINITY;
const f: number[][] = Array.from({ length: K + 1 }, () => Array(n).fill(inf));
f[0][0] = 0;
for (let k = 1; k <= K; k++) {
for (let j = 0; j < n; j++) {
f[k][j] = f[k - 1][j];
for (let i = 0; i < n; i++) {
if (flights[i][j]) {
f[k][j] = Math.max(f[k][j], f[k - 1][i]);
}
}
f[k][j] += days[j][k - 1];
}
}
return Math.max(...f[K]);
}
```

#### Rust

```rust
impl Solution {
pub fn max_vacation_days(flights: Vec<Vec<i32>>, days: Vec<Vec<i32>>) -> i32 {
let n = flights.len();
let k = days[0].len();
let inf = i32::MIN;

let mut f = vec![vec![inf; n]; k + 1];
f[0][0] = 0;

for step in 1..=k {
for j in 0..n {
f[step][j] = f[step - 1][j];
for i in 0..n {
if flights[i][j] == 1 {
f[step][j] = f[step][j].max(f[step - 1][i]);
}
}
f[step][j] += days[j][step - 1];
}
}

*f[k].iter().max().unwrap()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
24 changes: 24 additions & 0 deletions solution/0500-0599/0568.Maximum Vacation Days/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
impl Solution {
pub fn max_vacation_days(flights: Vec<Vec<i32>>, days: Vec<Vec<i32>>) -> i32 {
let n = flights.len();
let k = days[0].len();
let inf = i32::MIN;

let mut f = vec![vec![inf; n]; k + 1];
f[0][0] = 0;

for step in 1..=k {
for j in 0..n {
f[step][j] = f[step - 1][j];
for i in 0..n {
if flights[i][j] == 1 {
f[step][j] = f[step][j].max(f[step - 1][i]);
}
}
f[step][j] += days[j][step - 1];
}
}

*f[k].iter().max().unwrap()
}
}
19 changes: 19 additions & 0 deletions solution/0500-0599/0568.Maximum Vacation Days/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function maxVacationDays(flights: number[][], days: number[][]): number {
const n = flights.length;
const K = days[0].length;
const inf = Number.NEGATIVE_INFINITY;
const f: number[][] = Array.from({ length: K + 1 }, () => Array(n).fill(inf));
f[0][0] = 0;
for (let k = 1; k <= K; k++) {
for (let j = 0; j < n; j++) {
f[k][j] = f[k - 1][j];
for (let i = 0; i < n; i++) {
if (flights[i][j]) {
f[k][j] = Math.max(f[k][j], f[k - 1][i]);
}
}
f[k][j] += days[j][k - 1];
}
}
return Math.max(...f[K]);
}
2 changes: 0 additions & 2 deletions solution/0800-0899/0830.Positions of Large Groups/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ tags:
<strong>输出:</strong>[]
</pre>



<p><strong>提示:</strong></p>

<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心

我们可以每次贪心地选取一个不超过 $k$ 的最大的斐波那契数,然后将 $k$ 减去该数,答案加一,一直循环,直到 $k = 0$ 为止。

由于每次贪心地选取了最大的不超过 $k$ 的斐波那契数,假设为 $b$,前一个数为 $a$,后一个数为 $c$。将 $k$ 减去 $b$,得到的结果,一定小于 $a$,也即意味着,我们选取了 $b$ 之后,一定不会选到 $a$。这是因为,如果能选上 $a$,那么我们在前面就可以贪心地选上 $b$ 的下一个斐波那契数 $c$,这不符合我们的假设。因此,我们在选取 $b$ 之后,可以贪心地减小斐波那契数。

时间复杂度 $O(\log k)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -77,32 +83,40 @@ tags:
```python
class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
def dfs(k):
if k < 2:
return k
a = b = 1
while b <= k:
a, b = b, a + b
return 1 + dfs(k - a)

return dfs(k)
a = b = 1
while b <= k:
a, b = b, a + b
ans = 0
while k:
if k >= b:
k -= b
ans += 1
a, b = b - a, a
return ans
```

#### Java

```java
class Solution {

public int findMinFibonacciNumbers(int k) {
if (k < 2) {
return k;
}
int a = 1, b = 1;
while (b <= k) {
b = a + b;
a = b - a;
int c = a + b;
a = b;
b = c;
}
return 1 + findMinFibonacciNumbers(k - a);
int ans = 0;
while (k > 0) {
if (k >= b) {
k -= b;
++ans;
}
int c = b - a;
b = a;
a = c;
}
return ans;
}
}
```
Expand All @@ -113,80 +127,100 @@ class Solution {
class Solution {
public:
int findMinFibonacciNumbers(int k) {
if (k < 2) return k;
int a = 1, b = 1;
while (b <= k) {
b = a + b;
a = b - a;
int c = a + b;
a = b;
b = c;
}
return 1 + findMinFibonacciNumbers(k - a);
int ans = 0;
while (k > 0) {
if (k >= b) {
k -= b;
++ans;
}
int c = b - a;
b = a;
a = c;
}
return ans;
}
};
```

#### Go

```go
func findMinFibonacciNumbers(k int) int {
if k < 2 {
return k
}
func findMinFibonacciNumbers(k int) (ans int) {
a, b := 1, 1
for b <= k {
a, b = b, a+b
c := a + b
a = b
b = c
}
return 1 + findMinFibonacciNumbers(k-a)

for k > 0 {
if k >= b {
k -= b
ans++
}
c := b - a
b = a
a = c
}
return
}
```

#### TypeScript

```ts
const arr = [
1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
];

function findMinFibonacciNumbers(k: number): number {
let res = 0;
for (const num of arr) {
if (k >= num) {
k -= num;
res++;
if (k === 0) {
break;
}
let [a, b] = [1, 1];
while (b <= k) {
let c = a + b;
a = b;
b = c;
}

let ans = 0;
while (k > 0) {
if (k >= b) {
k -= b;
ans++;
}
let c = b - a;
b = a;
a = c;
}
return res;
return ans;
}
```

#### Rust

```rust
const FIB: [i32; 45] = [
1836311903, 1134903170, 701408733, 433494437, 267914296, 165580141, 102334155, 63245986,
39088169, 24157817, 14930352, 9227465, 5702887, 3524578, 2178309, 1346269, 832040, 514229,
317811, 196418, 121393, 75025, 46368, 28657, 17711, 10946, 6765, 4181, 2584, 1597, 987, 610,
377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1,
];

impl Solution {
pub fn find_min_fibonacci_numbers(mut k: i32) -> i32 {
let mut res = 0;
for &i in FIB.into_iter() {
if k >= i {
k -= i;
res += 1;
if k == 0 {
break;
}
let mut a = 1;
let mut b = 1;
while b <= k {
let c = a + b;
a = b;
b = c;
}

let mut ans = 0;
while k > 0 {
if k >= b {
k -= b;
ans += 1;
}
let c = b - a;
b = a;
a = c;
}
res
ans
}
}
```
Expand Down
Loading