Min Operation
Learn this problemProblem statement
The manager of an Amazon warehouse needs to ship n products from different locations, the location of the ith product is represented by an array locations[i]. The manager is allowed to perform one operation at a time. Each operation is described below:
Note: After shipping a product it gets removed from the inventory, and the rest of the products which are currently not shipped come together keeping the order the same as before.
Given n products and an array locations, find the minimum number of operations that the manager has to perform to ship all of the products.
Function
minOperation(m: int, locations: int[]) → intComplete the function minOperation in the editor.
minOperation has the following parameter(s):
int locations[n]: the location of each product.
Returns
int: the minimum number of operations that the manager has to perform to ship all of the products.
Examples
Example 1
m = 5locations = [1, 8, 6, 7, 7]return = 3One optimal sequence is to ship products from different locations together whenever possible. For [1, 8, 6, 7, 7], pair locations 1 and 8, then pair 6 and one 7, then ship the remaining 7 alone. This takes 3 operations.
Example 2
m = 4locations = [1, 3, 1, 2]return = 2Pair the products at locations 1 and 3, then pair the remaining products at locations 1 and 2. This ships all products in 2 operations.
Constraints
1 <= m <= 105locations.length == m1 <= locations[i] <= 109
More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026