Hard
,Array
,Greedy
,Heap
1675. Minimize Deviation in Array
You are given an array nums
of n
positive integers.
You can perform two types of operations on any element of the array any number of times:
2
.
[1,2,3,4]
, then you can do this operation on the last element, and the array will be [1,2,3,2]
.2
.
[1,2,3,4]
, then you can do this operation on the first element, and the array will be [2,2,3,4]
.The deviation of the array is the maximum difference between any two elements in the array.
Return the minimum deviation the array can have after performing some number of operations.
Example 1:
Example 2:
Example 3:
Constraints:
n
== nums.length
n
<= 5 * 104nums[i]
<= 109Time: 最多做n
次, 每次push一個值進priority queue
Extra Space:
XD Feb 24, 2023
跟天宇一樣,但考慮到數字是的倍數的話,那時間複雜度其實是
Runtime 602ms (Beats 38.66%)
Yen-Chi ChenSat, Feb 25, 2023
利用set來減少相同的數字重複檢查,複雜度一樣是,但執行時間能減少接近一半
Runtime 333ms (Beats 89.8%)
Yen-Chi ChenSat, Feb 25, 2023
Yen-Chi ChenSat, Feb 25, 2023
參考樓上兩位大神的方法1,先寫個max heap練習一下,結果錯了好幾次…
MarsgoatMar 2, 2023