#3510
Minimum Pair Removal to Sort Array II
hard · 39.2% accepted · 404 likes · top 18%
array · hash table · linked list · heap (priority queue) · simulation · doubly-linked list · ordered set
Description
Given an array nums, you can perform the following operation any number of times:
- Select the adjacent pair with the minimum sum in nums. If multiple such pairs exist, choose the leftmost one.
- Replace the pair with their sum.
Return the minimum number of operations needed to make the array non-decreasing.
An array is said to be non-decreasing if each element is greater than or equal to its previous element (if it exists).
Solution