FastPrepFastPrep
Problem Brief

Buy Volumes (Order Books :)

NEW GRADOA
See Amazon online assessment and hiring insights

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:

  • You have not purchased the volume before.
  • You already own all its direct prior volumes.
  • 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:

  • the volume numbers in ascending order if you purchased some volumes on the ith day
  • the single element -1 if you did not purchase any book
  • Function Description

    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~~!⭒𓆑 β­’ 🫧

    1Example 1

    Input
    volumes = [2, 1, 4, 3]
    Output
    [[-1], [1, 2], [-1], [3, 4]]
    Explanation
    Example 1 illustration
    Initially, all volumes are out of stock T~T On the first day, Volume No.2 becomes available. Since you don't own its prequel (Volume 1) yet, you will not purchase it. The answer for the first day is [-1]. On the second day, Volume N0.1 becomes available. Now, you can puchase both vol.1 and vol.2 together. The answer for the second day is [1, 2] On the third day, Volume No.4 becomes available. Since you do not have one of its prequels (Vol.3) yet, you will not buy Vol.4. The answer for the third day is [-1]. On the fourth day, Volume No.3 becomes available. Now, you can purchase both Vol.3 and Vol.4. The answer for the fourth day is [3, 4]. The final answer is [[-1], [1, 2],[-1], [3, 4]].

    2Example 2

    Input
    volumes = [1, 4, 3, 2, 5]
    Output
    [[1], [-1], [-1], [2, 3, 4], [5]]
    Explanation

    • 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]

    3Example 3

    Input
    volumes = [1, 2, 3]
    Output
    [[1], [2], [3]]
    Explanation
    Day 1, Volume 1 is released. Purchase volume 1. Day 2, Volume 2 is released. Purchase volume 2. Day 3, Volume 3 is released. Purchase volume 3. This example test case was added on 07-03-2025, many manyy thanks to the generous help a best friend offered!

    Constraints

    Limits and guarantees your solution can rely on.

  • 1 ≀ n ≀ 10^5
  • 1 ≀ volumes[i] ≀ n
  • Elements in volumes are distinct.
  • public int[][] buyVolumes(int[] volumes) {
      // write your code here
    }
    
    Input

    volumes

    [2, 1, 4, 3]

    Output

    [[-1], [1, 2], [-1], [3, 4]]

    Sign in to submit your solution.