[Python] short dp, explained
The idea is that we need to go back a lot of times. Define by dp[i] is number of days to reach cell i. We can only reach it from the cell i-1, so we need at lea
The idea is that we need to go back a lot of times. Define by `dp[i]` is number of days to reach cell `i`. We can only reach it from the cell `i-1`, so we need at least `dp[i-1]` steps. Then it will lead us to `dp[A[i-1]]` cell, because we visited `i-1` for the first time. Then we need to reach `i-1` from `A[i-1]` again. And finally we need one more step. \n\n#### Complexity\nIt is `O(n)` for time and space.\n\n#### Code\n```python\nclass Solution:\n    def firstDayBeenInAllRooms(self, A):\n        n, M = len(A), 10**9 + 7\n        dp = [0]*n\n        for i in range(1, n):\n            dp[i] = (2*dp[i-1] - dp[A[i-1]] + 2) % M\n\n        return dp[-1]  \n```\n\nIf you have any questions, feel free to ask. If you like solution and explanations, please **Upvote!**
['Dynamic Programming']