当前位置:网站首页>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
}
}
边栏推荐
- Install database -linux-5.7
- QT qstring:: number apply base conversion
- 用Redis实现分布式锁
- Construction of simple database learning environment
- Redis learning (I)
- 1922. Count Good Numbers
- 从0开始使用pnpm构建一个Monorepo方式管理的demo
- 1922. Count Good Numbers
- Jestson nano downloads updated kernel and DTB from TFTP server
- Powerdesign reverse wizard such as SQL and generates name and comment
猜你喜欢

LeetCode每日一题(2212. Maximum Points in an Archery Competition)

Directory and switching operation in file system

Using Hudi in idea

Please tell me how to set vscode

Django operates Excel files through openpyxl to import data into the database in batches.

Hudi learning notes (III) analysis of core concepts

Common software open source protocols
![[CSDN]C1训练题解析_第三部分_JS基础](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1训练题解析_第三部分_JS基础

Hudi quick experience (including detailed operation steps and screenshots)

NPM install installation dependency package error reporting solution
随机推荐
Derivation of Fourier transform
Spark 结构化流写入Hudi 实践
Please tell me how to set vscode
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
LeetCode每日一题(931. Minimum Falling Path Sum)
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
专利查询网站
Common software open source protocols
Spark 概述
unbuntu(debian)下TFTP服务器搭建及测试
Logstash+jdbc data synchronization +head display problems
Spark 集群安装与部署
Hudi data management and storage overview
Beego learning - Tencent cloud upload pictures
Crawler career from scratch (V): detailed explanation of re regular expression
[CSDN] C1 training problem analysis_ Part III_ JS Foundation
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)