FLIPKART Coding Question – Solved

2 Live
There are N trolleys used to transport products and each trolley is given a unique ID from 0 to N-1. A trolley operates for a certain time period and is able to carry products. For each trolley, the automated system stores three parameters i.e., the start and end time of operation (both inclusive) and units of products carried. A trolley can be operated within a specified period. No two trolleys can be operated during the same period, but if a trolley's operating period ends at point T, then another trolley can be immediately started at T. The system must store the maximum number of products transported. Write an algorithm to find the maximum number of products transported. Input The first line of the input consists of two space-separated integers - trolleyList_row and trolleyList_col, representing the number of trolleys (N) and the number of parameters associated with each trolley (trolleyList_col(M) is always equal to three). The next N lines consist of M space-separated integers representing the starting of the period, ending of the period and, the number of products carried by the trolley, respectively. Output Print an integer representing the maximum number of products transported. Constraints 0 ≤ trolleyList_row ≤ 10^4 0 ≤ starting of the period, ending of the period ≤ 1000 0 < number of products carried by the trolley ≤ 104 trolleyList_col = 3 Example Input: 4 3 0 2 4 0 4 9 2 4 6 5 10 20 Output: 30

Asked in: FLIPKART

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from sys import setrecursionlimit
setrecursionlimit(10**6)
import bisect
from functools import lru_cache
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, the key challenge is to find a set of trolleys that operate during non-overlapping time periods and yield the maximum total number of products transported. This is a classic optimization problem related to scheduling with weighted intervals, often known as the Weighted Interval Scheduling problem.

The problem specifics:
- Each trolley has a start time, an end time, and a weight (units of products carried).
- No two trolleys can be operated during overlapping intervals, but one trolley can start immediately after another finishes (end time of first equals start time of next).
- We want to maximize the sum of products transported by selecting compatible trolleys.

Step 1: Understand the problem model
This problem can be visualized as a timeline with intervals representing trolley operation times, each associated with a “weight” (products carried). The goal is to select intervals that do not overlap (except possibly touching at endpoints) and maximize the total weight.

Step 2: Sort intervals
To effectively solve the problem, start by sorting all trolleys by their end time in non-decreasing order. Sorting by end time is crucial because it allows for an incremental decision process, where you consider trolleys in order of finishing earliest to latest. This approach helps to decide which trolley to select by looking back at compatible intervals that finish before the current trolley starts.

Step 3: Define compatibility
For each trolley i, we need to find the latest trolley j (with j < i) such that trolley j’s operating period does not overlap with i’s. Since intervals may touch (end of j equals start of i), this is allowed, so compatibility means trolley j ends at or before trolley i starts.

Finding this compatible trolley for each trolley i can be done efficiently using a binary search on the sorted intervals since the intervals are sorted by end time.

Step 4: Dynamic programming formulation
Define an array dp, where dp[i] represents the maximum number of products transported using trolleys up to the i-th trolley in the sorted order.

To compute dp[i], consider two choices:
- Exclude trolley i: dp[i] = dp[i-1] (best without trolley i)
- Include trolley i: dp[i] = products[i] + dp[compatibleIndex], where compatibleIndex is the index of the last trolley that doesn’t overlap with trolley i

Then dp[i] = max(dp[i-1], products[i] + dp[compatibleIndex])

Base cases: dp[0] = 0 if we use 1-based indexing or for the first trolley, dp[1] = products[1].

The final answer will be dp[N], the maximum products considering all trolleys.

Step 5: Implementation details and optimizations
- Sorting intervals by end time enables binary search for finding compatible intervals, which improves performance.
- Using dynamic programming with binary search leads to a time complexity around O(N log N), suitable for up to 10^4 trolleys.
- Carefully handle the edge case where no compatible previous trolley exists (compatibleIndex = 0), and thus dp[compatibleIndex] = 0.
- When two intervals end and start at the same time, treat this as compatible, allowing the immediate start of the next trolley.

Step 6: Handling corner cases
- If no trolleys exist (N=0), the result is zero.
- Intervals that are completely contained inside others should be handled naturally by the DP process.
- Multiple trolleys with same start and end times but different product counts should be sorted and chosen based on maximum product gain.

Step 7: Result interpretation
The DP array accumulates the best possible transport units without overlapping intervals at each step. Once the entire array is computed, dp[N] gives the maximum number of products transported using the best compatible subset of trolleys.

Summary:
- Sort intervals by end time.
- For each trolley, find the last compatible trolley using binary search.
- Use dynamic programming to build up the maximum products transported by either including or excluding each trolley.
- Return the maximum sum after processing all trolleys.

This method ensures you choose trolleys to maximize product transportation while respecting the constraints of non-overlapping operational periods.
```


Related Questions