Problem · Array

Minimum Sorted Erasure Operations

Learn this problem
HardAtlassian logoAtlassianFULLTIMEOA

Problem statement

An array is stored as a mutable list, and a pointer initially selects its first element. One operation may move the pointer to the previous element, move it to the next element, or erase the selected element. After an erasure, choose the previous or next surviving neighbor as the new pointer when one exists.

Erase every element so that the erased values form a non-decreasing sequence. Return the minimum total number of operations, counting both pointer moves and erasures.

Function

minimumEraseOperations(arr: int[]) → long

Examples

Example 1

arr = [1]return = 1

Erase the only element.

Example 2

arr = [1,2,3,4]return = 4

Every current element is already the next required value, so four erasures are sufficient.

Example 3

arr = [2,1,3,3,2]return = 8

Move to and erase 1, erase the two 2 values while crossing the larger values, then erase the two 3 values.

Constraints

  • 1 <= arr.length <= 100000
  • 1 <= arr[i] <= 1000000000
  • The pointer does not wrap around either end of the list.

More Atlassian problems

drafts saved locally
public long minimumEraseOperations(int[] arr) {
  // Write your code here.
}
arr[1]
expected1
checking account