FastPrepFastPrep
Problem Brief

Min Operation

FULLTIMENEW GRADOA
See Amazon online assessment and hiring insights

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:

  • If the inventory has two or more products, the manager can pick two products x and y from the inventory if they have different locations (locations[x]!=locations[y]) and ship both of them.
  • If the inventory has one or more products, the manager can pick one product x from the inventory and ship it.
  • 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 Description

    Complete 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.

    1Example 1

    Input
    m = 5, locations = [1, 8, 6, 7, 7]
    Output
    3
    Explanation
    Example 1 illustration

    2Example 2

    Input
    m = 4, locations = [1, 3, 1, 2]
    Output
    2
    Explanation
    Example 2 illustration

    Constraints

    Limits and guarantees your solution can rely on.

  • 1 <= m <= 10^5
  • 1 <= locations[i] <= 10^9
  • public int minOperation(int m, int[] locations) {
      // write your code here
    }
    
    Input

    m

    5

    locations

    [1, 8, 6, 7, 7]

    Output

    3

    Sign in to submit your solution.