↓Skip to main content
  1. LeetCode/

LeetCode 3507: Minimum Pair Removal to Sort Array I

·2 mins· ·
LeetCode Easy Simulation Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-04-06

  • Submissions: Runtime Error → Accepted

Approach
#

The rule is fixed: find the adjacent pair with the smallest sum (leftmost on ties) and merge it into one number. Repeat until the array is non-decreasing and return the number of operations.

n <= 50, one scan per operation is O(n), and there are at most n operations, so direct O(n^2) simulation is enough.

The n = 10^5 version (LC 3510) is the one that needs a heap plus a doubly linked list.

My Attempt: assigning an int to a slice
#

nums[min_index:min_index+2] = min_value      # TypeError

The right side of a slice assignment must be iterable. To replace two elements with one, write [min_value].

Solution
#

class Solution:
    def minimumPairRemoval(self, nums: List[int]) -> int:
        ops = 0
        while any(nums[i] > nums[i + 1] for i in range(len(nums) - 1)):
            best = min(range(len(nums) - 1), key=lambda i: nums[i] + nums[i + 1])
            nums[best:best + 2] = [nums[best] + nums[best + 1]]
            ops += 1
        return ops

min returns the first of tied values, which matches “leftmost on ties”.

Complexity
#

Time O(n^2), space O(1).

Mistakes to Avoid
#

  • Putting a single value on the right side of a slice assignment.
  • Picking the right pair on ties: compare with <, not <=.

Interview Explanation
#

n is at most 50, so I simulate: while the array is not non-decreasing, find the adjacent pair with the smallest sum, leftmost on ties, replace it with the sum, and count the operation. Each operation is O(n) and there are at most n, so O(n^2) total. For large n, I would keep pair sums in a heap and use a doubly linked list for the merges.

Related

LeetCode 1646: Get Maximum in Generated Array
·2 mins
LeetCode Easy Array Simulation
LeetCode 1646 note: generate by the rules, nums[i // 2] at even i and nums[i // 2] + nums[i // 2 + 1] at odd i, handling n = 0 separately
LeetCode 867: Transpose Matrix
·1 min
LeetCode Easy Array Matrix Simulation
LeetCode 867 note: the transpose is n × m with res[j][i] = matrix[i][j]; in Python, zip(*matrix)
LeetCode 566: Reshape the Matrix
·2 mins
LeetCode Easy Array Matrix Simulation
LeetCode 566 note: return the original if the element counts differ; otherwise element k goes to (k // c, k % c)
LeetCode 495: Teemo Attacking
·2 mins
LeetCode Easy Array Simulation
LeetCode 495 note: each attack contributes min(gap to the next attack, duration), and the last one contributes the full duration
LeetCode 3069: Distribute Elements Into Two Arrays I
·1 min
LeetCode Easy Array Two-Pointers Simulation
LeetCode 3069 note: simulate the rule: the first two go to separate arrays, then each element joins the array whose last element is larger
LeetCode 682: Baseball Game
·1 min
LeetCode Easy Array Stack Simulation
LeetCode 682 note: keep valid scores on a stack; C removes the last, D adds its double, and + adds the sum of the last two