当前位置:网站首页>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;
}
}
边栏推荐
- How to switch nodejs versions at will?
- 【Unity3D】摇杆
- [kitex source code interpretation] service discovery
- 常用api
- VIM multiline operation
- Win10 wireless connection cannot input password characters, and it will be stuck as soon as it is input
- 线性回归——以一道等差数列的题为例
- PMP考试详解,新考纲有什么变化?
- 2022 Henan Mengxin League game (3): Henan University
- Continue to work hard on your skills, and the more you learn, the more you will learn
猜你喜欢

Relative path and absolute path

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

【一知半解】线程池

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

What is the PMP exam outline in 2022?

8.2 some algebraic knowledge (groups, cyclic groups and subgroups)

College personnel management system based on jsp+servlet

Are you suitable for automated testing?

Maximum sum of continuous subarray of sword finger offer (2)

如何组装一个注册中心
随机推荐
ssm练习第二天_项目拆分moudle_基本增删改查_批量删除_一对一级联查询
Leetcode 50 day question brushing plan (day 5 - longest palindrome substring 10.50-13:00)
数据库索引的原理,为什么要用 B+树,为什么不用二叉树?
【英雄哥七月集训】第 25天: 树状数组
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station
7月30号PMP考试延期后我们应该做什么?
同步时现实密码不匹配
Rookie cpaas platform microservice governance practice
Redis主从复制,读写分离,哨兵模式
剑指offer 连续子数组的最大和(二)
Sword finger offer regular expression matching
LeetCode_134_加油站
.net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
Linked list - the penultimate K nodes
Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)
成为测试/开发程序员,小张:现实就来了个下马威......
Hello World
效率提升98%!高海拔光伏电站运维巡检背后的AI利器
During the oppo interview, 16 questions were thrown over. I was stupid
Point cloud target detection Kitti dataset bin file visualization, one-stop solution