Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

์„ค๋ช…

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];
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ