当前位置:网站首页>LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
2022-07-03 09:01: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
太难表达了, 不写了, 看代码吧
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
}
}
边栏推荐
- ERROR: certificate common name “*.” doesn’t match requested ho
- [point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
- [point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
- Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
- 【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
- Use the interface colmap interface of openmvs to generate the pose file required by openmvs mvs
- Tag paste operator (#)
- Go language - Reflection
- Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
- Build a solo blog from scratch
猜你喜欢

How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?

AcWing 785. Quick sort (template)

Basic knowledge of network security

Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation

【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字

Sword finger offer II 029 Sorted circular linked list

Introduction to the basic application and skills of QT

Sword finger offer II 091 Paint the house
![[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds](/img/7d/b66545284d6baea2763fd8d8555e1d.png)
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds

LeetCode 871. Minimum refueling times
随机推荐
Hudi integrated spark data analysis example (including code flow and test results)
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Tag paste operator (#)
LeetCode 515. Find the maximum value in each tree row
Spark structured stream writing Hudi practice
[untitled] use of cmake
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
State compression DP acwing 91 Shortest Hamilton path
[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
Jenkins learning (I) -- Jenkins installation
Introduction to the basic application and skills of QT
Bert install no package metadata was found for the 'sacraments' distribution
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
Word segmentation in full-text indexing
Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
The less successful implementation and lessons of RESNET
Go language - JSON processing
Crawler career from scratch (V): detailed explanation of re regular expression