Skip to content

Commit 1fe2ac6

Browse files
authored
feat: add solutions to lc problem: No.1997 (doocs#2513)
No.1997.First Day Where You Have Been in All the Rooms
1 parent b6ec7d8 commit 1fe2ac6

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

solution/1900-1999/1997.First Day Where You Have Been in All the Rooms/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,32 @@ func firstDayBeenInAllRooms(nextVisit []int) int {
133133
}
134134
```
135135

136+
```ts
137+
function firstDayBeenInAllRooms(nextVisit: number[]): number {
138+
const n = nextVisit.length;
139+
const mod = 1e9 + 7;
140+
const f: number[] = new Array<number>(n).fill(0);
141+
for (let i = 1; i < n; ++i) {
142+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
143+
}
144+
return f[n - 1];
145+
}
146+
```
147+
148+
```cs
149+
public class Solution {
150+
public int FirstDayBeenInAllRooms(int[] nextVisit) {
151+
int n = nextVisit.Length;
152+
long[] f = new long[n];
153+
int mod = (int)1e9 + 7;
154+
for (int i = 1; i < n; ++i) {
155+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
156+
}
157+
return (int)f[n - 1];
158+
}
159+
}
160+
```
161+
136162
<!-- tabs:end -->
137163

138164
<!-- end -->

solution/1900-1999/1997.First Day Where You Have Been in All the Rooms/README_EN.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,17 @@ Day 6 is the first day where you have been in all the rooms.
6363

6464
## Solutions
6565

66-
### Solution 1
66+
### Solution 1: Dynamic Programming
67+
68+
We define $f[i]$ as the date number of the first visit to the $i$-th room, so the answer is $f[n - 1]$.
69+
70+
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$.
71+
72+
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$.
73+
74+
Finally, return $f[n-1]$.
75+
76+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of rooms.
6777

6878
<!-- tabs:start -->
6979

@@ -119,6 +129,32 @@ func firstDayBeenInAllRooms(nextVisit []int) int {
119129
}
120130
```
121131

132+
```ts
133+
function firstDayBeenInAllRooms(nextVisit: number[]): number {
134+
const n = nextVisit.length;
135+
const mod = 1e9 + 7;
136+
const f: number[] = new Array<number>(n).fill(0);
137+
for (let i = 1; i < n; ++i) {
138+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
139+
}
140+
return f[n - 1];
141+
}
142+
```
143+
144+
```cs
145+
public class Solution {
146+
public int FirstDayBeenInAllRooms(int[] nextVisit) {
147+
int n = nextVisit.Length;
148+
long[] f = new long[n];
149+
int mod = (int)1e9 + 7;
150+
for (int i = 1; i < n; ++i) {
151+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
152+
}
153+
return (int)f[n - 1];
154+
}
155+
}
156+
```
157+
122158
<!-- tabs:end -->
123159

124160
<!-- end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Solution {
2+
public int FirstDayBeenInAllRooms(int[] nextVisit) {
3+
int n = nextVisit.Length;
4+
long[] f = new long[n];
5+
int mod = (int)1e9 + 7;
6+
for (int i = 1; i < n; ++i) {
7+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
8+
}
9+
return (int)f[n - 1];
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function firstDayBeenInAllRooms(nextVisit: number[]): number {
2+
const n = nextVisit.length;
3+
const mod = 1e9 + 7;
4+
const f: number[] = new Array<number>(n).fill(0);
5+
for (let i = 1; i < n; ++i) {
6+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
7+
}
8+
return f[n - 1];
9+
}

0 commit comments

Comments
 (0)