-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1300-sum-of-mutated-array-closest-to-target.js
42 lines (36 loc) · 1.22 KB
/
1300-sum-of-mutated-array-closest-to-target.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 1300. Sum of Mutated Array Closest to Target
* https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/
* Difficulty: Medium
*
* Given an integer array arr and a target value target, return the integer value such that when
* we change all the integers larger than value in the given array to be equal to value, the
* sum of the array gets as close as possible (in absolute difference) to target.
*
* In case of a tie, return the minimum such integer.
*
* Notice that the answer is not neccesarilly a number from arr.
*/
/**
* @param {number[]} arr
* @param {number} target
* @return {number}
*/
var findBestValue = function(arr, target) {
arr.sort((a, b) => a - b);
let prefixSum = 0;
const length = arr.length;
for (let i = 0; i < length; i++) {
const remaining = length - i;
const avg = (target - prefixSum) / remaining;
if (avg <= arr[i]) {
const floorVal = Math.floor(avg);
const ceilVal = Math.ceil(avg);
const floorSum = prefixSum + floorVal * remaining;
const ceilSum = prefixSum + ceilVal * remaining;
return Math.abs(floorSum - target) <= Math.abs(ceilSum - target) ? floorVal : ceilVal;
}
prefixSum += arr[i];
}
return arr[length - 1];
};