Buy Volumes (Order Books :)
Learn this problemProblem statement
Amazon Books is a retail store that sells the newly launched novel "The Story of Amazon". The novel is divided into volumes numbered from 1 to n and unfortunately, all the volumes are currently out of stock.
The Amazon team announced that starting today, they will bring exactly one volume of "The Story of Amazon" in stock each of the next n days. On the nth day, all volumes will be there. Being an impatient bookworm, each day you will purchase the maximum number of volumes you can such that:
Note: For the ith volume of the novel, all the volumes such that j < i are its prequels.
Determine the volumes you would purchase each day. You should return an array of n arrays where the ith array contains:
Function
buyVolumes(volumes: int[]) → int[][]
Complete the function buyVolumes in the editor below.
buyVolumes has the following parameter:
int volumes[n]: an array of integers where the ith integer denotes the volume that is in stock on the ith day
Returns
int[n][n]: a 2d array of integers where the ith array denotes the volumes purchased on the ith day
𓈒𓇼 ࣪ 𓈒 Manyy Manyyy thanks to da best aikay and kl~~!⭒𓆡 ⭒ 🫧
Examples
Example 1
volumes = [2, 1, 4, 3]return = [[-1], [1, 2], [-1], [3, 4]]
Example 2
volumes = [1, 4, 3, 2, 5]return = [[1], [-1], [-1], [2, 3, 4], [5]]
- Day 1, Volume 1 is released. Purchase volume 1. answer[0] = [1]
- Day 2, Volume 4 is released. No volumes are purchased since you do not own all volumes 1 and 3. answer[1] = [-1]
- Day 3, Volume 3 is released. No volumes are purchased since you do not own volume 2. answer[2] = [-1]
- Day 4, Volume 2 is released. Purchase all three volumes. answer[3] = [2, 3, 4]
- Day 5, Volume 5 is released. Purchase volume 5. answer[4] = [5]
Example 3
volumes = [1, 2, 3]return = [[1], [2], [3]]Constraints
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