Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7df08f6

Browse files
committedApr 8, 2025
Add solution #1300
1 parent 7772719 commit 7df08f6

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,215 LeetCode solutions in JavaScript
1+
# 1,216 LeetCode solutions in JavaScript
22

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

@@ -983,6 +983,7 @@
983983
1297|[Maximum Number of Occurrences of a Substring](./solutions/1297-maximum-number-of-occurrences-of-a-substring.js)|Medium|
984984
1298|[Maximum Candies You Can Get from Boxes](./solutions/1298-maximum-candies-you-can-get-from-boxes.js)|Hard|
985985
1299|[Replace Elements with Greatest Element on Right Side](./solutions/1299-replace-elements-with-greatest-element-on-right-side.js)|Easy|
986+
1300|[Sum of Mutated Array Closest to Target](./solutions/1300-sum-of-mutated-array-closest-to-target.js)|Medium|
986987
1304|[Find N Unique Integers Sum up to Zero](./solutions/1304-find-n-unique-integers-sum-up-to-zero.js)|Easy|
987988
1309|[Decrypt String from Alphabet to Integer Mapping](./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js)|Easy|
988989
1313|[Decompress Run-Length Encoded List](./solutions/1313-decompress-run-length-encoded-list.js)|Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1300. Sum of Mutated Array Closest to Target
3+
* https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array arr and a target value target, return the integer value such that when
7+
* we change all the integers larger than value in the given array to be equal to value, the
8+
* sum of the array gets as close as possible (in absolute difference) to target.
9+
*
10+
* In case of a tie, return the minimum such integer.
11+
*
12+
* Notice that the answer is not neccesarilly a number from arr.
13+
*/
14+
15+
/**
16+
* @param {number[]} arr
17+
* @param {number} target
18+
* @return {number}
19+
*/
20+
var findBestValue = function(arr, target) {
21+
arr.sort((a, b) => a - b);
22+
let prefixSum = 0;
23+
const length = arr.length;
24+
25+
for (let i = 0; i < length; i++) {
26+
const remaining = length - i;
27+
const avg = (target - prefixSum) / remaining;
28+
29+
if (avg <= arr[i]) {
30+
const floorVal = Math.floor(avg);
31+
const ceilVal = Math.ceil(avg);
32+
const floorSum = prefixSum + floorVal * remaining;
33+
const ceilSum = prefixSum + ceilVal * remaining;
34+
35+
return Math.abs(floorSum - target) <= Math.abs(ceilSum - target) ? floorVal : ceilVal;
36+
}
37+
38+
prefixSum += arr[i];
39+
}
40+
41+
return arr[length - 1];
42+
};

0 commit comments

Comments
 (0)
Please sign in to comment.