-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1300. Sum of Mutated Array Closest to Target #213
Labels
Comments
Solution
/**
* @param {number[]} arr
* @param {number} target
* @return {number}
*/
var findBestValue = function(arr, target) {
arr.sort((a, b) => a - b);
const prefixs = new Array(arr.length);
let prefix = 0;
for (let i = 0; i < arr.length; i++) {
prefix += arr[i];
prefixs[i] = prefix;
}
let left = 0;
let right = arr[arr.length - 1];
let absoluteDifference = Infinity;
let result = 0;
while (left <= right) {
const mid = (left + right) >> 1;
const separator = sortedIndex(arr, mid);
const prefixSum = separator > 0 ? prefixs[separator - 1] : 0;
const sum = prefixSum + (arr.length - separator) * mid;
if (sum < target) {
left = mid + 1;
} else {
right = mid - 1;
}
const abs = Math.abs(target - sum);
if (abs < absoluteDifference) {
absoluteDifference = abs;
result = mid;
} else if (abs === absoluteDifference) {
result = Math.min(result, mid);
}
}
return result;
};
function sortedIndex(arr, value) {
const len = arr.length;
if (value > arr[arr.length - 1]) {
return arr.length;
}
let left = 0;
let right = len - 1;
while (left < right) {
const mid = (left + right) >> 1;
if (arr[mid] < value) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
function findBestValue(arr: number[], target: number): number {
arr.sort((a, b) => a - b);
const prefixs: number[] = new Array(arr.length);
let prefix = 0;
for (let i = 0; i < arr.length; i++) {
prefix += arr[i];
prefixs[i] = prefix;
}
let left = 0;
let right = arr[arr.length - 1];
let absoluteDifference = Infinity;
let result = 0;
while (left <= right) {
const mid = (left + right) >> 1;
const separator = sortedIndex(arr, mid);
const prefixSum = separator > 0 ? prefixs[separator - 1] : 0;
const sum = prefixSum + (arr.length - separator) * mid;
if (sum < target) {
left = mid + 1;
} else {
right = mid - 1;
}
const abs = Math.abs(target - sum);
if (abs < absoluteDifference) {
absoluteDifference = abs;
result = mid;
} else if (abs === absoluteDifference) {
result = Math.min(result, mid);
}
}
return result;
};
function sortedIndex(arr: number[], value: number): number {
const len = arr.length;
if (value > arr[arr.length - 1]) {
return arr.length;
}
let left = 0;
let right = len - 1;
while (left < right) {
const mid = (left + right) >> 1;
if (arr[mid] < value) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1300. Sum of Mutated Array Closest to Target
给你一个整数数组
arr
和一个目标值target
,请你返回一个整数value
,使得将数组中所有大于value
的值变成value
后,数组的和最接近target
(最接近表示两者之差的绝对值最小)。如果有多种使得和最接近
target
的方案,请你返回这些整数中的 最小值。请注意,答案不一定是
arr
中的数字。Example 1
Example 2
Example 3
Note
1 <= arr.length <= 10^4
1 <= arr[i], target <= 10^5
The text was updated successfully, but these errors were encountered: