Skip to content

Commit 160ad03

Browse files
chore: add LeetCode daily solution
1 parent de373b0 commit 160ad03

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Minimum Pair Removal to Sort Array II (Hard)
2+
3+
**Problem ID:** 3510
4+
**Date:** 2026-01-23
5+
**Link:** https://leetcode.com/problems/minimum-pair-removal-to-sort-array-ii/
6+
7+
## Approach
8+
9+
To solve the problem of determining the minimum number of operations needed to make an array non-decreasing via adjacent pair removal, we can follow these steps:
10+
11+
### Problem Understanding
12+
The goal is to repeatedly replace adjacent pairs with their sum until the array is non-decreasing. The key challenge is to select the pair with the minimum sum efficiently and ensure that the resultant array is sorted.
13+
14+
### Approach
15+
1. **Identify Non-Decreasing Pairs**: First, we need to identify pairs in the array that, when summed, will not disrupt the non-decreasing order. If a pair (nums[i], nums[i+1]) is such that nums[i] > nums[i+1], we need to consider replacing it.
16+
17+
2. **Priority Queue**: Use a min-heap (priority queue) to efficiently track pairs based on their sums. This allows us to always access the pair with the minimum sum quickly. The heap will store pairs along with their indices for easy retrieval.
18+
19+
3. **Operation Simulation**:
20+
- While the heap is not empty, extract the pair with the minimum sum.
21+
- Replace the extracted pair in the array with their sum.
22+
- After the replacement, check the new adjacent pairs to see if they now form a valid pair that can be summed in future operations.
23+
- Increment the operation count each time a replacement is made.
24+
25+
4. **Termination**: The process continues until no more pairs need to be replaced to achieve a non-decreasing order.
26+
27+
### Data Structures
28+
- **Min-Heap**: To maintain pairs and their sums efficiently.
29+
- **Array**: To hold the current state of the numbers as we perform operations.
30+
31+
### Complexity
32+
- **Time Complexity**: The overall complexity is O(n log n) due to the heap operations, where n is the length of the input array. Each operation involves inserting and removing elements from the heap.
33+
- **Space Complexity**: O(n) for storing the heap and potentially the modified array.
34+
35+
### Conclusion
36+
This approach effectively reduces the problem to a series of controlled operations based on the properties of the array and the sums of adjacent pairs, leveraging a priority queue for efficiency. By focusing on the minimum sum pairs, we ensure that each operation is optimal toward achieving a non-decreasing order in the least number of steps.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int minimumOperations(int[] nums) {
3+
int n = nums.length;
4+
int operations = 0;
5+
6+
while (!isNonDecreasing(nums)) {
7+
int minSum = Integer.MAX_VALUE;
8+
int minIndex = -1;
9+
10+
for (int i = 0; i < n - 1; i++) {
11+
int sum = nums[i] + nums[i + 1];
12+
if (sum < minSum) {
13+
minSum = sum;
14+
minIndex = i;
15+
}
16+
}
17+
18+
nums[minIndex] = nums[minIndex] + nums[minIndex + 1];
19+
for (int i = minIndex + 1; i < n - 1; i++) {
20+
nums[i] = nums[i + 1];
21+
}
22+
n--;
23+
operations++;
24+
}
25+
26+
return operations;
27+
}
28+
29+
private boolean isNonDecreasing(int[] nums) {
30+
for (int i = 1; i < nums.length; i++) {
31+
if (nums[i] < nums[i - 1]) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var minimumOperations = function(nums) {
2+
let operations = 0;
3+
let n = nums.length;
4+
5+
while (true) {
6+
let minSum = Infinity;
7+
let minIndex = -1;
8+
9+
for (let i = 0; i < n - 1; i++) {
10+
if (nums[i] + nums[i + 1] < minSum) {
11+
minSum = nums[i] + nums[i + 1];
12+
minIndex = i;
13+
}
14+
}
15+
16+
if (minIndex === -1) break;
17+
18+
let newValue = nums[minIndex] + nums[minIndex + 1];
19+
nums[minIndex] = newValue;
20+
nums.splice(minIndex + 1, 1);
21+
operations++;
22+
23+
if (isSorted(nums)) break;
24+
}
25+
26+
return operations;
27+
};
28+
29+
function isSorted(arr) {
30+
for (let i = 1; i < arr.length; i++) {
31+
if (arr[i] < arr[i - 1]) return false;
32+
}
33+
return true;
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def minimumOperations(self, nums: List[int]) -> int:
3+
operations = 0
4+
while True:
5+
min_sum = float('inf')
6+
min_index = -1
7+
8+
for i in range(len(nums) - 1):
9+
if nums[i] + nums[i + 1] < min_sum:
10+
min_sum = nums[i] + nums[i + 1]
11+
min_index = i
12+
13+
if min_index == -1 or (min_index > 0 and nums[min_index - 1] > nums[min_index] + nums[min_index + 1]) or (min_index < len(nums) - 2 and nums[min_index] + nums[min_index + 1] > nums[min_index + 2]):
14+
break
15+
16+
nums[min_index] = nums[min_index] + nums[min_index + 1]
17+
del nums[min_index + 1]
18+
operations += 1
19+
20+
return operations

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
342342
- 2026-01-20 — [Construct the Minimum Bitwise Array I](https://leetcode.com/problems/construct-the-minimum-bitwise-array-i/) (Easy) → `Easy/2026-01-20-3314-Construct-the-Minimum-Bitwise-Array-I`
343343
- 2026-01-21 — [Construct the Minimum Bitwise Array II](https://leetcode.com/problems/construct-the-minimum-bitwise-array-ii/) (Medium) → `Medium/2026-01-21-3315-Construct-the-Minimum-Bitwise-Array-II`
344344
- 2026-01-22 — [Minimum Pair Removal to Sort Array I](https://leetcode.com/problems/minimum-pair-removal-to-sort-array-i/) (Easy) → `Easy/2026-01-22-3507-Minimum-Pair-Removal-to-Sort-Array-I`
345+
- 2026-01-23 — [Minimum Pair Removal to Sort Array II](https://leetcode.com/problems/minimum-pair-removal-to-sort-array-ii/) (Hard) → `Hard/2026-01-23-3510-Minimum-Pair-Removal-to-Sort-Array-II`

0 commit comments

Comments
 (0)