[PS] 99ํด๋ฝ ์ฝํ ์คํฐ๋ 42์ผ์ฐจ TIL (First Day Where You Have Been in All the Rooms)
ํ๊ทธ: 99ํด๋ฝ, PS, TIL, ๋์ ๊ณํ๋ฒ, ์ฝ๋ฉํ ์คํธ์ค๋น, ํญํด99
์นดํ ๊ณ ๋ฆฌ: PS
๋ฌธ์
์ค๋ช
1
2
3
4
5
6
7
8
// [0, 0, 2]
// on day 0, visit room 0 => 1(odd), next visit : nextVisit[0] = 0
// on day 1, visit room 0 => 2(even), next visit : (0 + 1) % 3 = 1 (0 -> 1)
// on day 2, visit room 1 => 1(odd), next visit : nextVisit[1] = 0 (1 -> 0)
// on day 3, visit room 0 => 3(odd), next visit : nextVisit[0] = 0
// on day 4, visit room 0 => 4(even), next visit : (0 + 1) % 3 = 1
// on day 5, visit room 1 => 2(even), next visit : (1 + 1) % 3 = 2
// on day 6, visit room 2 => 1(odd), end
ํ์ด
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int firstDayBeenInAllRooms(int[] nextVisit) {
int n = nextVisit.length;
long[] dp = new long[n];
int mod = (int) 1e9 + 7;
dp[0] = 0; // ์์ ๋ฐฉ์ด๊ธฐ ๋๋ฌธ์ ์ฒซ๋ ์ 0
for (int i = 1; i < n; i++) {
// dp[i] = ์ด์ ๋ฐฉ๊น์ง ๊ฑธ๋ฆฐ ์๊ฐ + ํ์ฌ ๋ฐฉ์ ๋์ฐฉํ๋ ์๊ฐ + ๋ค์ ๋์๊ฐ๋ ์๊ฐ
dp[i] = (dp[i - 1] + 1 + dp[i - 1] - dp[nextVisit[i - 1]] + 1 + mod) % mod;
}
return (int) dp[n - 1];
}
}
๋๊ธ๋จ๊ธฐ๊ธฐ