Simple DP O(n) 22ms beating 100%
IntuitionUse DP to go from left to right.Approach
Create an array table[] whose length = nextVisit.length.
table[0] = 0; table[1] = 2;
For index i, table[i] = 2
# Intuition
Use DP to go from left to right.
# Approach
1. Create an array table[] whose length = nextVisit.length.
2. table[0] = 0; table[1] = 2;
3. For index `i`, table[i] = 2 + table[i - 1] + table[i - 1] - table[nextVisit[i - 1]].
4. Don't forget to simplify the above formula, and apply the modulo operation to it.
5. Return table.at(-1)
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(n)
# Code
```javascript []
/**
 * @param {number[]} nextVisit
 * @return {number}
 */
var firstDayBeenInAllRooms = function(nextVisit) {
    const modMe = 10 ** 9 + 7
    const len = nextVisit.length
    const modMePlus2 = modMe + 2


    const table = new Array(len)
    table[0] = 0
    table[1] = 2
    for (let i = 2; i < len; i++) {
        const prevOutcome = table[i - 1]
        const prevPointee = nextVisit[i - 1]
        const outcome = (modMePlus2 + 2 * prevOutcome - table[prevPointee]) % modMe

        table[i] = outcome
    }


    const result = table.at(-1)
    return result
};
```
['Dynamic Programming', 'JavaScript']