Skip to content

feat: add solutions to lc problem: No.1997 #2513

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
Mar 28, 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 @@ -133,6 +133,32 @@ func firstDayBeenInAllRooms(nextVisit []int) int {
}
```

```ts
function firstDayBeenInAllRooms(nextVisit: number[]): number {
const n = nextVisit.length;
const mod = 1e9 + 7;
const f: number[] = new Array<number>(n).fill(0);
for (let i = 1; i < n; ++i) {
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
}
return f[n - 1];
}
```

```cs
public class Solution {
public int FirstDayBeenInAllRooms(int[] nextVisit) {
int n = nextVisit.Length;
long[] f = new long[n];
int mod = (int)1e9 + 7;
for (int i = 1; i < n; ++i) {
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
}
return (int)f[n - 1];
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ Day 6 is the first day where you have been in all the rooms.

## Solutions

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

We define $f[i]$ as the date number of the first visit to the $i$-th room, so the answer is $f[n - 1]$.

Consider the date number of the first arrival at the $(i-1)$-th room, denoted as $f[i-1]$. At this time, it takes one day to return to the $nextVisit[i-1]$-th room. Why return? Because the problem restricts $0 \leq nextVisit[i] \leq i$.

After returning, the $nextVisit[i-1]$-th room is visited an odd number of times, and the rooms from $nextVisit[i-1]+1$ to $i-1$ are visited an even number of times. At this time, we go to the $(i-1)$-th room again from the $nextVisit[i-1]$-th room, which takes $f[i-1] - f[nextVisit[i-1]]$ days, and then it takes one more day to reach the $i$-th room. Therefore, $f[i] = f[i-1] + 1 + f[i-1] - f[nextVisit[i-1]] + 1$. Since $f[i]$ may be very large, we need to take the remainder of $10^9 + 7$, and to prevent negative numbers, we need to add $10^9 + 7$.

Finally, return $f[n-1]$.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -119,6 +129,32 @@ func firstDayBeenInAllRooms(nextVisit []int) int {
}
```

```ts
function firstDayBeenInAllRooms(nextVisit: number[]): number {
const n = nextVisit.length;
const mod = 1e9 + 7;
const f: number[] = new Array<number>(n).fill(0);
for (let i = 1; i < n; ++i) {
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
}
return f[n - 1];
}
```

```cs
public class Solution {
public int FirstDayBeenInAllRooms(int[] nextVisit) {
int n = nextVisit.Length;
long[] f = new long[n];
int mod = (int)1e9 + 7;
for (int i = 1; i < n; ++i) {
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
}
return (int)f[n - 1];
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Solution {
public int FirstDayBeenInAllRooms(int[] nextVisit) {
int n = nextVisit.Length;
long[] f = new long[n];
int mod = (int)1e9 + 7;
for (int i = 1; i < n; ++i) {
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
}
return (int)f[n - 1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function firstDayBeenInAllRooms(nextVisit: number[]): number {
const n = nextVisit.length;
const mod = 1e9 + 7;
const f: number[] = new Array<number>(n).fill(0);
for (let i = 1; i < n; ++i) {
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
}
return f[n - 1];
}
Loading