Similar Problems

Similar Problems not available

Minimum Time To Complete Trips - Leetcode Solution

Companies:

LeetCode:  Minimum Time To Complete Trips Leetcode Solution

Difficulty: Medium

Topics: binary-search array  

Problem:

You are given n trips (numbered from 0 to n-1) and a 2D array trips where trips[i] = [startTime_i, endTime_i, passengersCount_i] indicates that the ith trip started at startTime_i and ended at endTime_i with passengersCount_i passengers.

You have a train that can travel on the s single track of length 1 unit. This train travels from any point to any other point on the track and it takes 1 unit of time to reach its destination. However, the train cannot pass other trips or stop during a trip.

Let's say the train starts at the position pos and has an initial speed of speed units per second, which means that:

  • The train moves 1 unit of distance to the right each second.
  • If the train is at a position x different from pos, it takes |x - pos| * speed seconds to reach position x.

The train can choose its initial position, but once it starts moving, it cannot stop until it reaches the end of the track.

The train should find the minimum time needed to visit all the trips. You can assume that the starting speed of the train is such that it can visit all the trips.

Return the minimum time in seconds needed to visit all the trips.

Example 1:

Input: trips = [[9,12,5],[3,9,4],[4,6,5],[12,15,5],[7,11,3]], speed = 5, starting position = 2 Output: 34 Explanation: The best strategy is:

  • Starting at position 2:
  • Go to trip 1. Time needed: current position / speed = 2 / 5 = 0.4 seconds.
  • Go to trip 2. Time needed: (9 - 2) / 5 = 1.4 seconds.
  • Pick up all passengers. Time needed: 0 seconds.
  • Go to trip 3. Time needed: (4 - 9) / 5 = 1 seconds.
  • Drop off all passengers. Time needed: 0 seconds.
  • Go to trip 0. Time needed: (6 - 4) / 5 = 0.4 seconds.
  • Pick up all passengers. Time needed: 0 seconds.
  • Go to trip 4. Time needed: (7 - 6) / 5 = 0.2 seconds.
  • Pick up all passengers. Time needed: 0 seconds.
  • Go to trip 3. Time needed: (12 - 7) / 5 = 1 seconds.
  • Drop off all passengers. Time needed: 0 seconds.
  • Go to trip 4. Time needed: (12 - 7) / 5 = 1 seconds.
  • Drop off all passengers. Time needed: 0 seconds.
  • Go to the end. Time needed: (15 - 12) / 5 = 0.6 seconds.
  • Total time needed: 0.4 + 1.4 + 1 + 0.4 + 0.2 + 1 + 1 + 0.6 = 6 seconds.

Solution:

To solve the problem, we can use binary search to find the minimum time required to complete all the trips. We can assume that the time required is within the range [0, max_end_time], where max_end_time is the maximum end time among all the trips.

For each value of time in the range [0, max_end_time], we can simulate the train journey starting from each position on the single track.

For each starting position and speed, we can calculate the time required to complete all the trips, using dynamic programming.

The state of the dynamic programming is represented as a tuple (pos, speed, i), where pos is the current position of the train on the single track, speed is the current speed of the train, and i is the index of the next trip to visit.

The base case of the dynamic programming is when i = n, which means all the trips have been visited. The cost of this state is 0.

The transition of the dynamic programming is defined as follows:

For each trip j such that endTime_j <= time and i <= j, we can consider two options:

  • If the train has not yet picked up any passengers at trip j, we can pick up all the passengers and move to the next trip. The cost of this option is the time required to move from pos to startTime_j, plus the time required to pick up all the passengers and move to the next trip, plus the cost of the next state (pos = endTime_j, speed = 1, i = j + 1).
  • If the train has already picked up the passengers at trip j, we can move directly to the next trip. The cost of this option is the time required to move from pos to endTime_j, plus the cost of the next state (pos = endTime_j, speed = 1, i = j + 1).

The final answer is the minimum cost among all the starting positions and speeds.

The time complexity of this approach is O(n^3 log(max_end_time)), where n is the number of trips and max_end_time is the maximum end time among all the trips. The space complexity is O(n^2).

Code:

Here is the Python code for the solution:

class Solution: def minTime(self, trips: List[List[int]], speed: int, start: int) -> int: n = len(trips) max_end_time = max(trips, key=lambda x: x[1])[1]

    def dp(pos, speed, i, memo):
        if i == n:
            return 0
        
        if (pos, speed, i) in memo:
            return memo[(pos, speed, i)]
        
        time = trips[i][0]
        min_cost = float('inf')
        for j in range(i, n):
            if trips[j][1] <= time:
                cost_1 = abs(trips[j][0] - pos) / speed + dp(trips[j][1], 1, j+1, memo)
                cost_2 = (trips[j][1] - pos) / speed + dp(trips[j][1], 1, j+1, memo)
                min_cost = min(min_cost, cost_1, cost_2)
            else:
                break
        
        memo[(pos, speed, i)] = min_cost
        return min_cost
    
    left, right = 0, max_end_time
    while left < right:
        mid = (left + right) // 2
        if dp(start, speed, 0, {}) <= mid:
            right = mid
        else:
            left = mid + 1
    
    return left

Minimum Time To Complete Trips Solution Code

1