Optimized dynamic programming approach.
Complexity\n- Time complexity:\nO(n), where n is the length of the nextVisit array.\n\n- Space complexity:\nO(n), where n is the length of the nextVisit array.\
# Complexity\n- Time complexity:\nO(**n**), where **n** is the length of the *nextVisit* array.\n\n- Space complexity:\nO(**n**), where **n** is the length of the *nextVisit* array.\n\n# Code\n```\n/**\n * @param {number[]} nextVisit\n * @return {number}\n */\nvar firstDayBeenInAllRooms = function (nextVisit) {\n    const mod = (10 ** 9) + 7;\n    const n = nextVisit.length;\n    const days = new Array(n);\n    days[0] = 1;\n    days[1] = 2;\n    let curr;\n    for (let i = 2; i < n; i++) {\n        curr = 2 * days[i - 1] + 1 - days[nextVisit[i - 1]];\n        if (curr < 0) curr += mod; // this condition makes the solution faster.\n        curr %= mod;\n        days[i] = curr;\n    }\n\n    return ((days.at(-1) - 1) * 2) % mod;\n};\n```
['Array', 'Dynamic Programming', 'JavaScript']