当前位置:网站首页>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
}
}
边栏推荐
- [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)
- Recommend a low code open source project of yyds
- Jenkins learning (III) -- setting scheduled tasks
- Spark 结构化流写入Hudi 实践
- Crawler career from scratch (V): detailed explanation of re regular expression
- Digital statistics DP acwing 338 Counting problem
- WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
- Go language - JSON processing
- [point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
- Integrated use of interlij idea and sonarqube
猜你喜欢
![[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis](/img/fa/36d28b754a9f380bfd86d4562268c3.png)
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis

MySQL installation and configuration (command line version)

LeetCode 532. K-diff number pairs in array

Hudi 快速体验使用(含操作详细步骤及截图)

Data mining 2021-4-27 class notes

What is an excellent fast development framework like?

LeetCode 30. Concatenate substrings of all words
![[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion](/img/76/b92fe4549cacba15c113993a07abb8.png)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion

Introduction to the basic application and skills of QT

【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
随机推荐
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
Go language - Reflection
Detailed steps of windows installation redis
Overview of image restoration methods -- paper notes
Database execution error: SQL_ mode only_ full_ group_ by:
传统企业数字化转型需要经过哪几个阶段?
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
Data mining 2021-4-27 class notes
Word segmentation in full-text indexing
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
What are the stages of traditional enterprise digital transformation?
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
Low code momentum, this information management system development artifact, you deserve it!
【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
Liteide is easy to use
The less successful implementation and lessons of RESNET
Logstash+jdbc data synchronization +head display problems
2022-1-6 Niuke net brush sword finger offer
C language programming specification
Just graduate student reading thesis