Skip to content

Commit 9452047

Browse files
committed
Add solution #3422
1 parent 0d04ea5 commit 9452047

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,6 +2690,7 @@
26902690
3405|[Count the Number of Arrays with K Matching Adjacent Elements](./solutions/3405-count-the-number-of-arrays-with-k-matching-adjacent-elements.js)|Hard|
26912691
3406|[Find the Lexicographically Largest String From the Box II](./solutions/3406-find-the-lexicographically-largest-string-from-the-box-ii.js)|Hard|
26922692
3416|[Subsequences with a Unique Middle Mode II](./solutions/3416-subsequences-with-a-unique-middle-mode-ii.js)|Hard|
2693+
3422|[Minimum Operations to Make Subarray Elements Equal](./solutions/3422-minimum-operations-to-make-subarray-elements-equal.js)|Medium|
26932694
3423|[Maximum Difference Between Adjacent Elements in a Circular Array](./solutions/3423-maximum-difference-between-adjacent-elements-in-a-circular-array.js)|Easy|
26942695
3439|[Reschedule Meetings for Maximum Free Time I](./solutions/3439-reschedule-meetings-for-maximum-free-time-i.js)|Medium|
26952696
3440|[Reschedule Meetings for Maximum Free Time II](./solutions/3440-reschedule-meetings-for-maximum-free-time-ii.js)|Medium|
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* 3422. Minimum Operations to Make Subarray Elements Equal
3+
* https://leetcode.com/problems/minimum-operations-to-make-subarray-elements-equal/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums and an integer k. You can perform the following operation
7+
* any number of times:
8+
* - Increase or decrease any element of nums by 1.
9+
*
10+
* Return the minimum number of operations required to ensure that at least one subarray of size
11+
* k in nums has all elements equal.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var minOperations = function(nums, k) {
20+
const n = nums.length;
21+
const minHeap = new PriorityQueue((a, b) => a[0] < b[0] ? -1 : 1);
22+
const maxHeap = new PriorityQueue((a, b) => a[0] > b[0] ? -1 : 1);
23+
const maxHeapSize = Math.ceil(k / 2);
24+
const minHeapSize = k - maxHeapSize;
25+
26+
let total = 0;
27+
for (let i = 0; i < k; i++) {
28+
total += nums[i];
29+
maxHeap.enqueue([nums[i], i]);
30+
}
31+
32+
let minSum = 0;
33+
const minHeapIndices = new Set();
34+
for (let i = 0; i < minHeapSize; i++) {
35+
const [num, idx] = maxHeap.dequeue();
36+
minSum += num;
37+
minHeap.enqueue([num, idx]);
38+
minHeapIndices.add(idx);
39+
}
40+
41+
let maxSum = total - minSum;
42+
let median = maxHeap.front()[0];
43+
let result = Math.abs(median * maxHeapSize - maxSum) + Math.abs(minSum - median * minHeapSize);
44+
45+
for (let i = k; i < n; i++) {
46+
const num = nums[i];
47+
const leftOut = i - k;
48+
total += num - nums[leftOut];
49+
50+
while (!minHeap.isEmpty() && minHeap.front()[1] <= leftOut) minHeap.dequeue();
51+
while (!maxHeap.isEmpty() && maxHeap.front()[1] <= leftOut) maxHeap.dequeue();
52+
53+
if (minHeapIndices.has(leftOut)) {
54+
minHeapIndices.delete(leftOut);
55+
minSum -= nums[leftOut];
56+
maxHeap.enqueue([num, i]);
57+
const [newNum, newIdx] = maxHeap.dequeue();
58+
minSum += newNum;
59+
minHeap.enqueue([newNum, newIdx]);
60+
minHeapIndices.add(newIdx);
61+
} else {
62+
minHeap.enqueue([num, i]);
63+
minSum += num;
64+
minHeapIndices.add(i);
65+
const [newNum, newIdx] = minHeap.dequeue();
66+
minHeapIndices.delete(newIdx);
67+
minSum -= newNum;
68+
maxHeap.enqueue([newNum, newIdx]);
69+
}
70+
71+
maxSum = total - minSum;
72+
while (!maxHeap.isEmpty() && maxHeap.front()[1] <= leftOut) maxHeap.dequeue();
73+
median = maxHeap.front()[0];
74+
result = Math.min(
75+
result,
76+
Math.abs(median * maxHeapSize - maxSum) + Math.abs(minSum - median * minHeapSize)
77+
);
78+
}
79+
80+
return result;
81+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy