File tree 2 files changed +44
-1
lines changed
2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,215 LeetCode solutions in JavaScript
1
+ # 1,216 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
983
983
1297|[ Maximum Number of Occurrences of a Substring] ( ./solutions/1297-maximum-number-of-occurrences-of-a-substring.js ) |Medium|
984
984
1298|[ Maximum Candies You Can Get from Boxes] ( ./solutions/1298-maximum-candies-you-can-get-from-boxes.js ) |Hard|
985
985
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|
986
987
1304|[ Find N Unique Integers Sum up to Zero] ( ./solutions/1304-find-n-unique-integers-sum-up-to-zero.js ) |Easy|
987
988
1309|[ Decrypt String from Alphabet to Integer Mapping] ( ./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js ) |Easy|
988
989
1313|[ Decompress Run-Length Encoded List] ( ./solutions/1313-decompress-run-length-encoded-list.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments