当前位置:网站首页>LeetCode_ 1005_ Maximized array sum after K negations
LeetCode_ 1005_ Maximized array sum after K negations
2022-07-26 18:25:00 【Fitz1318】
Topic link
Title Description
Give you an array of integers nums And an integer k , Modify the array as follows :
Select a subscript i And will nums[i] Replace with -nums[i] .
Repeating this process happens to k Time . You can select the same subscript multiple times i.
After modifying the array in this way , Returns an array of The maximum possible and .
Example 1:
Input :nums = [4,2,3], k = 1
Output :5
explain : Select subscript 1 ,nums Turn into [4,-2,3] .
Example 2:
Input :nums = [3,-1,0,2], k = 3
Output :6
explain : Select subscript (1, 2, 2) ,nums Turn into [3,1,0,2] .
Example 3:
Input :nums = [2,-3,-1,5,-4], k = 2
Output :13
explain : Select subscript (1, 4) ,nums Turn into [2,3,-1,5,4] .
Tips :
1 <= nums.length <= 10^4-100 <= nums[i] <= 1001 <= k <= 104
Their thinking
The law of greed
- Sort the array before each inversion , Just let the smallest take the opposite
AC Code
class Solution {
public int largestSumAfterKNegations(int[] nums, int k) {
int ans = 0;
for (int i = 0; i < k; i++) {
Arrays.sort(nums);
nums[0] = -nums[0];
}
for (int num : nums) {
ans += num;
}
return ans;
}
}
边栏推荐
- Distributed link tracking Jaeger's use in golang
- drools-基础语法
- Netease game R & D Engineer Intern (client side)
- Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
- 7、 Common commands of ROS (II): rosservice, rossrv, rosparam
- The second set of 2020 American Asian individual match
- Hello World
- ICML 2022(第四篇)|| 图分层对齐图核实现图匹配
- Hello World
- SSH based online mall
猜你喜欢

Oracle day 2 (Views, indexes, PLSQL, cursors, stored procedures and stored functions, triggers, JDBC access stored procedures and stored functions)

相对路径与绝对路径

Day 4 of SSM practice_ Get user name_ User exit_ User CRUD_ Password encryption_ Roles_ jurisdiction

剑指offer 连续子数组的最大和(二)

MySQL 遇到过死锁问题吗,你是如何解决的?

Linked list - the penultimate K nodes

面试OPPO,16道题甩过来,我人傻了

LeetCode50天刷题计划(Day 4—— 最长回文子串 14.00-16:20)

2022年的PMP考试大纲是什么?

Distributed link tracking Jaeger's use in golang
随机推荐
LeetCode 0137. 只出现一次的数字 II
Leetcode 0139. word splitting
Linux安装mysql8.0.29详细教程
Leetcode 0137. number II that appears only once
同步时现实密码不匹配
J9数字论:如何避免踩雷多头陷阱?
Bulletgraph (bullet diagram, bullet diagram)
6、 Common commands of ROS (I): rosnode, rostopic, rosmsg
链表-两个链表的第一个公共结点
If the recommendation effect is not satisfactory, it's better to try to learn the propeller chart
.net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
数据库索引的原理,为什么要用 B+树,为什么不用二叉树?
Day 4 of SSM practice_ Get user name_ User exit_ User CRUD_ Password encryption_ Roles_ jurisdiction
Sword finger offer regular expression matching
详解 gRPC 客户端长连接机制实现
【英雄哥七月集训】第 25天: 树状数组
Leetcode 50 day question brushing plan (day 5 - longest palindrome substring 10.50-13:00)
PMP考试详解,新考纲有什么变化?
Distributed link tracking Jaeger's use in golang
Linux Installation mysql8.0.29 detailed tutorial