当前位置:网站首页>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
}
}
边栏推荐
- Go language - Reflection
- Equality judgment of long type
- 数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
- Win10 quick screenshot
- Matlab dichotomy to find the optimal solution
- Utilisation de hudi dans idea
- Win10 install elk
- The idea of compiling VBA Encyclopedia
- Jetson nano custom boot icon kernel logo CBOOT logo
- CATIA automation object architecture - detailed explanation of application objects (III) systemservice
猜你喜欢
Hudi学习笔记(三) 核心概念剖析
Please tell me how to set vscode
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
NPM install installation dependency package error reporting solution
【Kotlin学习】类、对象和接口——定义类继承结构
Run flash demo on ECS
Common software open source protocols
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
The rise and fall of mobile phones in my perspective these 10 years
Crawler career from scratch (IV): climb the bullet curtain of station B through API
随机推荐
专利查询网站
Navicat, MySQL export Er graph, er graph
LeetCode每日一题(516. Longest Palindromic Subsequence)
Jenkins learning (I) -- Jenkins installation
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
Filter comments to filter out uncommented and default values
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Beego learning - Tencent cloud upload pictures
LeetCode每日一题(1996. The Number of Weak Characters in the Game)
[solution to the new version of Flink without bat startup file]
PolyWorks script development learning notes (4) - data import and alignment using file import
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
Spark structured stream writing Hudi practice
Utilisation de hudi dans idea
unbuntu(debian)下TFTP服务器搭建及测试
Overview of image restoration methods -- paper notes
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
Jenkins learning (II) -- setting up Chinese
Flink学习笔记(八)多流转换