当前位置:网站首页>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
}
}
边栏推荐
- Discussion on enterprise informatization construction
- Introduction to the basic application and skills of QT
- Utilisation de hudi dans idea
- 【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
- 传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
- WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
- Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
- [point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
- Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
- [untitled] use of cmake
猜你喜欢

Principles of computer composition - cache, connection mapping, learning experience
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说

Hudi integrated spark data analysis example (including code flow and test results)

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

Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!

Just graduate student reading thesis

State compression DP acwing 91 Shortest Hamilton path

Move anaconda, pycharm and jupyter notebook to mobile hard disk

Sword finger offer II 091 Paint the house

Recommend a low code open source project of yyds
随机推荐
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Navicat, MySQL export Er graph, er graph
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
Computing level network notes
[point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
State compression DP acwing 91 Shortest Hamilton path
[kotlin learning] classes, objects and interfaces - define class inheritance structure
LeetCode 57. Insert interval
The "booster" of traditional office mode, Building OA office system, was so simple!
Bert install no package metadata was found for the 'sacraments' distribution
Derivation of Fourier transform
【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
IDEA 中使用 Hudi
Banner - Summary of closed group meeting
【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
Just graduate student reading thesis