Skip to content

Commit c9fee53

Browse files
committed
Add solution #1675
1 parent 27d4b5f commit c9fee53

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,467 LeetCode solutions in JavaScript
1+
# 1,468 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1291,6 +1291,7 @@
12911291
1672|[Richest Customer Wealth](./solutions/1672-richest-customer-wealth.js)|Easy|
12921292
1673|[Find the Most Competitive Subsequence](./solutions/1673-find-the-most-competitive-subsequence.js)|Medium|
12931293
1674|[Minimum Moves to Make Array Complementary](./solutions/1674-minimum-moves-to-make-array-complementary.js)|Medium|
1294+
1675|[Minimize Deviation in Array](./solutions/1675-minimize-deviation-in-array.js)|Hard|
12941295
1679|[Max Number of K-Sum Pairs](./solutions/1679-max-number-of-k-sum-pairs.js)|Medium|
12951296
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
12961297
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* 1675. Minimize Deviation in Array
3+
* https://leetcode.com/problems/minimize-deviation-in-array/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array nums of n positive integers.
7+
*
8+
* You can perform two types of operations on any element of the array any number of times:
9+
* - If the element is even, divide it by 2.
10+
* - For example, if the array is [1,2,3,4], then you can do this operation on the last element,
11+
* and the array will be [1,2,3,2].
12+
* - If the element is odd, multiply it by 2.
13+
* - For example, if the array is [1,2,3,4], then you can do this operation on the first element,
14+
* and the array will be [2,2,3,4].
15+
*
16+
* The deviation of the array is the maximum difference between any two elements in the array.
17+
*
18+
* Return the minimum deviation the array can have after performing some number of operations.
19+
*/
20+
21+
/**
22+
* @param {number[]} nums
23+
* @return {number}
24+
*/
25+
var minimumDeviation = function(nums) {
26+
const heap = [];
27+
let minElement = Infinity;
28+
29+
for (const num of nums) {
30+
const value = num % 2 ? num * 2 : num;
31+
heap.push(value);
32+
minElement = Math.min(minElement, value);
33+
}
34+
35+
for (let i = Math.floor(heap.length / 2) - 1; i >= 0; i--) {
36+
siftDown(i);
37+
}
38+
39+
let minDeviation = heap[0] - minElement;
40+
41+
while (heap[0] % 2 === 0) {
42+
const maxElement = heap[0];
43+
heap[0] = maxElement / 2;
44+
minElement = Math.min(minElement, heap[0]);
45+
siftDown(0);
46+
minDeviation = Math.min(minDeviation, heap[0] - minElement);
47+
}
48+
49+
return minDeviation;
50+
51+
function siftDown(index) {
52+
while (2 * index + 1 < heap.length) {
53+
let maxChild = 2 * index + 1;
54+
if (maxChild + 1 < heap.length && heap[maxChild + 1] > heap[maxChild]) {
55+
maxChild++;
56+
}
57+
if (heap[index] < heap[maxChild]) {
58+
[heap[index], heap[maxChild]] = [heap[maxChild], heap[index]];
59+
index = maxChild;
60+
} else {
61+
break;
62+
}
63+
}
64+
}
65+
};

0 commit comments

Comments
 (0)