[C++] Start and End times
Start time: Day when we visit node i for the first time\nEnd time: Day when we visit node i for the first time after Start time\n\nWe will calculate Start and E
**Start time**: *Day when we visit node i for the first time*\n**End time**: *Day when we visit node i for the first time after Start time*\n\nWe will calculate Start and End times for all nodes from 0 to n-1 in the following way - \n\n**Start time:**\nstart[i] = end[i-1] + 1 for i>0 (this is true because we can only visit node i when we have visited the previous node (i-1) two times) \nand start[0] = 0\n\n**End time:**\nend[i] = start[i] + (start[i] - start[nums[i]] +1) \nThis is because we want to know after how much time we will we able to revisit the node i once we start from node nums[i],  (start[i] - start[nums[i]] +1) is the number of days to revisit node i, if we start from node nums[i].\n\nCode:\n```\nclass Solution {\npublic:\n\t// kulyash\'s code\n    int firstDayBeenInAllRooms(vector<int>& nums) {\n        int n=nums.size();\n        long long M=1000000007;\n        vector<int>end(n,0),start(n,0);\n        for(int i=0;i<n-1;i++){\n            if(i!=0)start[i]=end[i-1]+1;\n            end[i]=2*start[i]-start[nums[i]]+1;\n            end[i]=(end[i]+M)%M;\n        }\n        return (end[n-2]+1)%M;\n    }\n};\n```\n\nThe final output is end[n-2] + 1 which is same as start[n-1]!\n\n
['Dynamic Programming', 'C']