当前位置:网站首页>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
}
}
边栏推荐
- 【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
- Explanation of the answers to the three questions
- 【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
- AcWing 787. Merge sort (template)
- Hudi 快速体验使用(含操作详细步骤及截图)
- 2022-2-14 learning xiangniuke project - generate verification code
- [point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
- Django operates Excel files through openpyxl to import data into the database in batches.
- 低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
- 【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points
猜你喜欢

传统企业数字化转型需要经过哪几个阶段?

Introduction to the basic application and skills of QT

LeetCode 508. The most frequent subtree elements and

LeetCode 1089. Duplicate zero

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

Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code

2022-2-14 learning the imitation Niuke project - send email

LeetCode 715. Range module

Build a solo blog from scratch
[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
随机推荐
Navicat, MySQL export Er graph, er graph
【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points
Hudi learning notes (III) analysis of core concepts
With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
Derivation of Fourier transform
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
Crawler career from scratch (IV): climb the bullet curtain of station B through API
2022-2-13 learn the imitation Niuke project - Project debugging skills
LeetCode 535. Encryption and decryption of tinyurl
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
Powerdesign reverse wizard such as SQL and generates name and comment
State compression DP acwing 91 Shortest Hamilton path
LeetCode 715. Range module
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
LeetCode 75. Color classification
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字