当前位置:网站首页>1300. sum of varied array closed to target
1300. sum of varied array closed to target
2022-07-03 09:33:00 【wangjun861205】
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.
Example 1:
Input: arr = [4,9,3], target = 10
Output: 3
Explanation: When using 3 arr converts to [3, 3, 3] which sums 9 and that’s the optimal answer.
Example 2:
Input: arr = [2,3,5], target = 10
Output: 5
Example 3:
Input: arr = [60864,25176,27249,21296,20204], target = 56803
Output: 11361
Constraints:
- 1 <= arr.length <= 104
- 1 <= arr[i], target <= 105
It's too hard to express , No more , Look at the code
impl Solution {
pub fn find_best_value(mut arr: Vec<i32>, target: i32) -> i32 {
arr.sort();
let ori = arr.clone();
let mut total = arr.len() as i32;
let arr = arr.into_iter().fold(Vec::new(), |mut l, v| {
if let Some((last_val, mut last_count)) = l.pop() {
if last_val == v {
last_count += 1;
l.push((last_val, last_count));
return l;
}
l.push((last_val, last_count));
}
l.push((v, 1));
return l;
});
let mut sum = 0;
let mut diffs = Vec::new();
let mut prev = i32::MIN;
for (v, c) in arr {
if target < sum {
break;
}
let d = (target - sum) / total;
if d <= v && d > prev {
diffs.push((((sum + d * total) - target).abs(), d));
}
if d + 1 <= v && d + 1 > prev {
diffs.push((((sum + (d + 1) * total) - target).abs(), d + 1));
}
sum += v * c;
total -= c;
prev = v;
}
if diffs.is_empty() {
return *ori.last().unwrap();
}
diffs.sort();
diffs[0].1
}
}
边栏推荐
- Database execution error: SQL_ mode only_ full_ group_ by:
- 全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
- [untitled] use of cmake
- LeetCode每日一题(2305. Fair Distribution of Cookies)
- Computing level network notes
- Jestson Nano 从tftp服务器下载更新kernel和dtb
- Win10 install elk
- Trial of the combination of RDS and crawler
- QT qstring:: number apply base conversion
- Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
猜你喜欢
![[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords](/img/ee/d982fd9e1f2283e09ad1a81d0b61b5.png)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords

Run flash demo on ECS

Crawler career from scratch (IV): climb the bullet curtain of station B through API

Vscode编辑器右键没有Open In Default Browser选项
![[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks](/img/61/aa8d0067868ce9e28cadf5369cd65e.png)
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks

CATIA automation object architecture - detailed explanation of application objects (I) document/settingcontrollers

Django operates Excel files through openpyxl to import data into the database in batches.

Jenkins learning (II) -- setting up Chinese

IDEA 中使用 Hudi

全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
随机推荐
Desktop icon recognition based on OpenCV
Notes on numerical analysis (II): numerical solution of linear equations
About the configuration of vs2008+rade CATIA v5r22
Global KYC service provider advance AI in vivo detection products have passed ISO international safety certification, and the product capability has reached a new level
Go language - JSON processing
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
Utilisation de hudi dans idea
Esp32 at command does not respond
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
Solve POM in idea Comment top line problem in XML file
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
Install database -linux-5.7
The server denied password root remote connection access
Computing level network notes
Vscode编辑器右键没有Open In Default Browser选项
[kotlin learning] classes, objects and interfaces - define class inheritance structure
IDEA 中使用 Hudi