Skip to content

Commit 917feae

Browse files
committed
feat: add solutions to lc problem: No.1997
No.1997.First Day Where You Have Been in All the Rooms
1 parent f828762 commit 917feae

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,82 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:动态规划**
71+
72+
我们定义 $f[i]$ 表示第一次访问第 $i$ 号房间的日期编号,那么答案就是 $f[n - 1]$。
73+
74+
我们考虑第一次到达第 $i-1$ 号房间的日期编号,记为 $f[i-1]$,此时需要花一天的时间回退到第 $nextVisit[i-1]$ 号房间,为什么是回退呢?因为题目限制了 $0 \leq nextVisit[i] \leq i$。
75+
76+
回退之后,此时第 $nextVisit[i-1]$ 号房间的访问为奇数次,而第 $[nextVisit[i-1]+1,..i-1]$ 号房间均被访问偶数次,那么这时候我们从第 $nextVisit[i-1]$ 号房间再次走到第 $i-1$ 号房间,就需要花费 $f[i-1] - f[nextVisit[i-1]]$ 天的时间,然后再花费一天的时间到达第 $i$ 号房间,因此 $f[i] = f[i-1] + 1 + f[i-1] - f[nextVisit[i-1]] + 1$。由于 $f[i]$ 可能很大,因此需要对 $10^9 + 7$ 取余,并且为了防止负数,需要加上 $10^9 + 7$。
77+
78+
最后返回 $f[n-1]$ 即可。
79+
80+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为房间数。
81+
7082
<!-- tabs:start -->
7183

7284
### **Python3**
7385

7486
<!-- 这里可写当前语言的特殊实现逻辑 -->
7587

7688
```python
77-
89+
class Solution:
90+
def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:
91+
n = len(nextVisit)
92+
f = [0] * n
93+
mod = 10**9 + 7
94+
for i in range(1, n):
95+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1) % mod
96+
return f[-1]
7897
```
7998

8099
### **Java**
81100

82101
<!-- 这里可写当前语言的特殊实现逻辑 -->
83102

84103
```java
104+
class Solution {
105+
public int firstDayBeenInAllRooms(int[] nextVisit) {
106+
int n = nextVisit.length;
107+
long[] f = new long[n];
108+
final int mod = (int) 1e9 + 7;
109+
for (int i = 1; i < n; ++i) {
110+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
111+
}
112+
return (int) f[n - 1];
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int firstDayBeenInAllRooms(vector<int>& nextVisit) {
123+
int n = nextVisit.size();
124+
vector<long long> f(n);
125+
const int mod = 1e9 + 7;
126+
for (int i = 1; i < n; ++i) {
127+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
128+
}
129+
return f[n - 1];
130+
}
131+
};
132+
```
85133
134+
### **Go**
135+
136+
```go
137+
func firstDayBeenInAllRooms(nextVisit []int) int {
138+
n := len(nextVisit)
139+
f := make([]int, n)
140+
const mod = 1e9 + 7
141+
for 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+
}
86146
```
87147

88148
### **...**

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,61 @@ Day 6 is the first day where you have been in all the rooms.
6666
### **Python3**
6767

6868
```python
69-
69+
class Solution:
70+
def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:
71+
n = len(nextVisit)
72+
f = [0] * n
73+
mod = 10**9 + 7
74+
for i in range(1, n):
75+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1) % mod
76+
return f[-1]
7077
```
7178

7279
### **Java**
7380

7481
```java
82+
class Solution {
83+
public int firstDayBeenInAllRooms(int[] nextVisit) {
84+
int n = nextVisit.length;
85+
long[] f = new long[n];
86+
final int mod = (int) 1e9 + 7;
87+
for (int i = 1; i < n; ++i) {
88+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
89+
}
90+
return (int) f[n - 1];
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int firstDayBeenInAllRooms(vector<int>& nextVisit) {
101+
int n = nextVisit.size();
102+
vector<long long> f(n);
103+
const int mod = 1e9 + 7;
104+
for (int i = 1; i < n; ++i) {
105+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
106+
}
107+
return f[n - 1];
108+
}
109+
};
110+
```
75111
112+
### **Go**
113+
114+
```go
115+
func firstDayBeenInAllRooms(nextVisit []int) int {
116+
n := len(nextVisit)
117+
f := make([]int, n)
118+
const mod = 1e9 + 7
119+
for i := 1; i < n; i++ {
120+
f[i] = (f[i-1] + 1 + f[i-1] - f[nextVisit[i-1]] + 1 + mod) % mod
121+
}
122+
return f[n-1]
123+
}
76124
```
77125

78126
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int firstDayBeenInAllRooms(vector<int>& nextVisit) {
4+
int n = nextVisit.size();
5+
vector<long long> f(n);
6+
const int mod = 1e9 + 7;
7+
for (int i = 1; i < n; ++i) {
8+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
9+
}
10+
return f[n - 1];
11+
}
12+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func firstDayBeenInAllRooms(nextVisit []int) int {
2+
n := len(nextVisit)
3+
f := make([]int, n)
4+
const mod = 1e9 + 7
5+
for 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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int firstDayBeenInAllRooms(int[] nextVisit) {
3+
int n = nextVisit.length;
4+
long[] f = new long[n];
5+
final 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:
3+
n = len(nextVisit)
4+
f = [0] * n
5+
mod = 10**9 + 7
6+
for i in range(1, n):
7+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1) % mod
8+
return f[-1]

0 commit comments

Comments
 (0)