Python brute force and optimized
Brute Force\nHaving a set to check until we reach all nodes and use the definitions given in question. Takes O(days) time and O(n) space. Gives TLE bcoz days ca
**Brute Force**\nHaving a set to check until we reach all nodes and use the definitions given in question. Takes O(days) time and O(n) space. Gives TLE bcoz days can be huge.\n```\nclass Solution:\n    def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:\n        mod = 1000000007\n        n = len(nextVisit)\n        times = [0]*n\n        s = set()\n        day = 0\n        room = 0\n        \n        while len(s) < n:\n            s.add(room)\n            day += 1\n            times[room] += 1\n            if times[room] % 2 == 1:\n                room = nextVisit[room]\n            else:\n                room = (room+1)%n\n            \n        return (day-1)%mod\n```\n\n**Better approach**\nInstead of traversing each day precompute the days for reaching from i th room to next room and from next room to i th room since we can move to next room only when its even number of visits.\n```\nclass Solution:\n    def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:\n        n, mod = len(nextVisit), 1000000007\n        dp = [0]*n\n        for i in range(1, n):\n            dp[i] = (2*dp[i-1] - dp[nextVisit[i-1]] + 2) % mod\n\n        return dp[-1]\n```
['Dynamic Programming', 'Python']