当前位置:网站首页>【贪心】leetcode1005K次取反后数组后的最大值
【贪心】leetcode1005K次取反后数组后的最大值
2022-06-21 15:33:00 【暮色_年华】
Given an integer array nums and an integer k, modify the array in the following way:
choose an index i and replace nums[i] with -nums[i].
You should apply this process exactly k times. You may choose the same index i multiple times.
Return the largest possible sum of the array after modifying it in this way.

1 <= nums.length <= 1e4-100 <= nums[i] <= 1001 <= k <= 1e4
题意:定义操作:nums[i]改为-nums[i],进行k次操作(同一个数可以进行多次操作),求数组的最大和
思路:
首先对元素进行排序,因为把一个负数变为正数时,总和变大,所有优先考虑操作负数;
当负数越小,操作后收益越大,所以优先考虑操作绝对值大的负数;
当负数的个数大于k时,只需要操作k次负数;
当负数的个数小于k时,把负数全部操作完后只能操作非负数,操作后总和只能变小或者不变。
(1)如果操作后k的值变为偶数,那么可以相当于不操作,总和不变。
(2)如果操作后k的值变为奇数,那么相当于只操作了一次,那么优先操作绝对值最小的数。
注意:
因为长度1e4,范围-100~100所以优先考虑使用桶排序
class Solution {
public int largestSumAfterKNegations(int[] nums, int k) {
Map<Integer,Integer>map=new HashMap<>();
int t=(int)1e8;
for(int n:nums){
map.put(n,map.getOrDefault(n,0)+1);
t=Math.min(t,Math.abs(n));
}
for(int i=-100;i<0&&k>0;i++){
while(map.getOrDefault(i,0)>0&&k>0){
map.put(i,map.get(i)-1);
map.put(-i,map.getOrDefault(-i,0)+1);
k--;
}
}
int res=0;
for(int i=-100;i<=100;i++){
res+=i*map.getOrDefault(i,0);
}
if(k%2!=0)res-=2*t;
return res;
}
}边栏推荐
- [cicadaplayer] read and write of HLS stream
- ConnectionOptions. Username attribute definition
- The application of RPC technology and its framework sekiro in crawler reverse, encrypting data is a shuttle!
- Gather high-quality ar application developers, and help the AR field prosper with technology
- What is Objective-C ID in swift- What is the equivalent of an Objective-C id in Swift?
- Write static multi data source code and do scheduled tasks to realize database data synchronization
- Telnet batch test (I): pit between telnet graceful stop and firewall
- Phantom star VR product details 34: Happy pitching
- 模拟设计磁盘文件的链接存储结构
- 理财产品预约赎回确认日是什么?
猜你喜欢

原生JS路由,iframe框架

Phantom star VR product details 34: Happy pitching
![Analysis of China's social financing scale and financing structure in 2021: RMB loans to the real economy account for more than 60%[figure]](/img/93/218ba91dc6e9866e186c004ac18e0a.jpg)
Analysis of China's social financing scale and financing structure in 2021: RMB loans to the real economy account for more than 60%[figure]

MNIST model training (with code)

GO语言-接口

For the first time in China, Tsinghua and other teams won the wsdm2022 only best paper award, and Hong Kong Chinese won the "time test Award"

Algorithm question: interview question 32 - I. print binary tree from top to bottom (title + idea + code + comments) sequence traversal time and space 1ms to beat 97.84% of users once AC

A pit trodden in the equivalence comparison of integer
![[Yugong series] February 2022 wechat applet -app Debug JSON configuration attribute](/img/33/103a207dc085c431557981b3c9a1d5.jpg)
[Yugong series] February 2022 wechat applet -app Debug JSON configuration attribute

Fluent encapsulates an immersive navigation bar with customizable styles NavigationBar
随机推荐
New project template of punctual atom F103 based on firmware library
Analysis of China's social financing scale and financing structure in 2021: RMB loans to the real economy account for more than 60%[figure]
Brain: machine learning reveals two different neuroanatomical subtypes of schizophrenia
我的Debug之路1.0
Idea restart
Taishan Office Technology Lecture: storage structure of domain in model
Shell uses arrays
模拟设计磁盘文件的链接存储结构
Go admin framework analysis (2-1)
Description of new features and changes in ABP Framework version 5.3.0
Xiao Lan does experiments (count the number of primes)
Which service provider is cheaper to do website penetration testing
Get the mobile number of QQ friends through exhaustive search
GO语言-结构体
Go language -type keyword
Implementation of asynchronous request pool
Angular 服务器端渲染应用的开箱即用的缓存功能问题
Learn upward management and four questioning skills to get twice the result with half the effort
Gee Registration Guide
What is PDT