Visit All Places in Minimum Time
<Removed due to DMCA complaint>
The Idea: First, it is important to know what sort of problem we can reduce this too. Thinking abstractly, our goal is to find the minimum subarray in A that contains set(A). In other words, our goal is to try and find a minimum window in A that contains all the elements (vacation locations) in A.
In our algorithm, two pointers need to be maintained that form the current window (that doesn't optimally represent the solution), and a result window, which eventually will represent the optimal solution.
Initially, we need n vacation locations to visit, where n is the total amount of unique vacation location. Now comes the core algorithm: for every vacation location, we add it to our current window. We will continue to do this until the current window contains all of the required vacation days. The moment this happens one of the constraints of the problem is satisfied, but the start of our window could contain some redundant results. For example, in the process we may have encounter more than 1 vacation day. So our next task is to get rid of as much as possible from the start of the window as long as our current window contains all of the required vacation days. This will satisfy the second constraint (which is to minimize the number the total number of days for travel).
Complexity: O(2n) time and O(n) space
Last updated