FastPrepFastPrep
Problem Brief

Get Min Operations

FULLTIMEOA
See Amazon online assessment and hiring insights

🍊 Following is the original prompt - πŸ₯‘

Devs at AMZ are working on a new sorting algorithm for points on the x-axis of the coordinate system.

There are n points. The ith point initially has a weight of weight[i] and is located at position i on the x-axis. In a single position, the ith point can be moved to the right by a distance of dist[i].

Given weight and dist, find the minimum number of operations required to sort the points by their weights.

Function Description πŸ₯‘

Complete the function getMinOperations2 in the editor -

GetMinOperations2 has the following arguments -

  • int weights[n]: the weights of the points
  • int dist[n]: the distances the points can move
  • Returns

    long int: the min num of operations to sort the points. Here long int represents a 64 bit integer. :)

    1Example 1

    Input
    weight = [3, 6, 5, 2], dist = [4, 3, 2, 1]
    Output
    5
    Explanation
    Example 1 illustration
    Thus, the number of operations required are 1 + 2 + 2 = 5.

    2Example 2

    Input
    weight = [2, 4, 3, 1], dist = [2, 6, 3, 5]
    Output
    4
    Explanation
    Perform the ops on the first point twice and the second and the third points once. The final points are [4, 7, 3, 5] :)

    Constraints

    Limits and guarantees your solution can rely on.

  • 2 <= n <= 2 * 105
  • 1 <= weights[i] <= 109
  • 1 <= dist[i] <= 103
  • public long getMinOperations2(int[] weight, int[] dist) {
      // write your code here
    }
    
    Input

    weight

    [3, 6, 5, 2]

    dist

    [4, 3, 2, 1]

    Output

    5

    Sign in to submit your solution.