Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3b1c655

Browse files
committed
Add solution #1997
1 parent 5208f50 commit 3b1c655

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,677 LeetCode solutions in JavaScript
1+
# 1,678 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1532,6 +1532,7 @@
15321532
1994|[The Number of Good Subsets](./solutions/1994-the-number-of-good-subsets.js)|Hard|
15331533
1995|[Count Special Quadruplets](./solutions/1995-count-special-quadruplets.js)|Easy|
15341534
1996|[The Number of Weak Characters in the Game](./solutions/1996-the-number-of-weak-characters-in-the-game.js)|Medium|
1535+
1997|[First Day Where You Have Been in All the Rooms](./solutions/1997-first-day-where-you-have-been-in-all-the-rooms.js)|Medium|
15351536
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
15361537
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15371538
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 1997. First Day Where You Have Been in All the Rooms
3+
* https://leetcode.com/problems/first-day-where-you-have-been-in-all-the-rooms/
4+
* Difficulty: Medium
5+
*
6+
* There are n rooms you need to visit, labeled from 0 to n - 1. Each day is labeled, starting
7+
* from 0. You will go in and visit one room a day.
8+
*
9+
* Initially on day 0, you visit room 0. The order you visit the rooms for the coming days is
10+
* determined by the following rules and a given 0-indexed array nextVisit of length n:
11+
* - Assuming that on a day, you visit room i,
12+
* - if you have been in room i an odd number of times (including the current visit), on the next
13+
* day you will visit a room with a lower or equal room number specified by nextVisit[i]
14+
* where 0 <= nextVisit[i] <= i;
15+
* - if you have been in room i an even number of times (including the current visit), on the
16+
* next day you will visit room (i + 1) mod n.
17+
*
18+
* Return the label of the first day where you have been in all the rooms. It can be shown
19+
* that such a day exists. Since the answer may be very large, return it modulo 109 + 7.
20+
*/
21+
22+
/**
23+
* @param {number[]} nextVisit
24+
* @return {number}
25+
*/
26+
var firstDayBeenInAllRooms = function(nextVisit) {
27+
const MOD = 1e9 + 7;
28+
const n = nextVisit.length;
29+
const dp = new Array(n).fill(0);
30+
31+
for (let i = 1; i < n; i++) {
32+
dp[i] = (2 * dp[i - 1] + 2 - dp[nextVisit[i - 1]] + MOD) % MOD;
33+
}
34+
35+
return dp[n - 1];
36+
};

0 commit comments

Comments
 (0)