16. Minimum Time
The problem can be found at the following link: Question Link
My Approach
To find the minimum time required to collect fruits from different locations, I have used the following approach:
First, I create a container called
col
to store the minimum and maximum values for each fruit type.Then, I create a vector called
type
to store the different types of fruits we have.Using a loop, I iterate through the locations and types. For each fruit type, I update the minimum and maximum values in the
col
container.Additionally, I add type 0 to
col
with a value of 0, since we start from 0, and type 1000002 with a value of 0, since we end at 0.Next, I iterate through the
col
container and store the different fruit types in thetype
vector.I calculate the total number of fruit types we have,
tsz
.Using the
dp
array, I implement a tabulation-based dynamic programming approach to optimize the solution. Thedp
array is initialized to store the minimum time for each fruit type and position.I iterate through the
type
vector in reverse order and calculate the minimum time required to reach the end from each position.Finally, I return the minimum time between the two possible starting positions,
dp[0][0]
anddp[0][1]
.
Time and Auxiliary Space Complexity
Time Complexity:
O(N)
, whereN
is the number of locations. The loop to process the locations and types runs inO(N)
time. The loop to calculate the minimum time using dynamic programming also takesO(N)
time.Auxiliary Space Complexity:
O(N)
since we use additional space to store thecol
container and thetype
vector.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated