【leetcode】1005. Maximize Sum Of Array After K Negations
题目如下:
Given an array
A
of integers, we must modify the array in the following way: we choose ani
and replaceA[i]
with-A[i]
, and we repeat this processK
times in total. (We may choose the same indexi
multiple times.)Return the largest possible sum of the array after modifying it in this way.
Example 1:
Input: A = [4,2,3], K = 1
Output: 5
Explanation: Choose indices (1,) and A becomes [4,-2,3].Example 2:
Input: A = [3,-1,0,2], K = 3
Output: 6
Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2].Example 3:
Input: A = [2,-3,-1,5,-4], K = 2
Output: 13
Explanation: Choose indices (1, 4) and A becomes [2,3,-1,5,4].Note:
1 <= A.length <= 10000
1 <= K <= 10000
-100 <= A[i] <= 100
解题思路:把A按升序排序,如果存在负数,那么优先把较大的负数变成正数,直到K次变换用换为止。如果K大于负数的个数,这时A中已经全部是正数了,我们知道对一个数变换两次相当于不做变换,因此只要判断剩余的可变换次数是奇数还是偶数。如果是偶数,那表示不用再做变换;如果是奇数,则把A中最小的正数做变换。最后求出A的总和即可。
代码如下:
class Solution(object):
def largestSumAfterKNegations(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
A.sort()
res = 0
lastMin = 10001
for i in A:
if K == 0:
res += i
elif i < 0:
res += (-i)
K -= 1
lastMin = min(lastMin,-i)
else:
K = K%2
if K == 1:
if lastMin < i:
res -= 2*lastMin
res += i
else:
res -= i
K = 0
else:
res += i
return res
【leetcode】1005. Maximize Sum Of Array After K Negations的更多相关文章
- 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- Leetcode 1005. Maximize Sum Of Array After K Negations
class Solution(object): def largestSumAfterKNegations(self, A, K): """ :type A: List[ ...
- LeetCode.1005-K次取负数组和最大(Maximize Sum Of Array After K Negations)
这是悦乐书的第376次更新,第403篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第237题(顺位题号是1005).给定一个整数数组A,我们必须按以下方式修改数组:我们选 ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
- [Swift]LeetCode1005. K 次取反后最大化的数组和 | Maximize Sum Of Array After K Negations
Given an array A of integers, we must modify the array in the following way: we choose an i and repl ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 多线程模拟生产者消费者示例之Lock
public class Test { public static void main(String[] args) { List<String> list = new ArrayList ...
- 替换OSD操作的优化与分析
http://www.zphj1987.com/2016/09/19/%E6%9B%BF%E6%8D%A2OSD%E6%93%8D%E4%BD%9C%E7%9A%84%E4%BC%98%E5%8C%9 ...
- 【2019 Multi-University Training Contest 4】
01:https://www.cnblogs.com/myx12345/p/11644076.html 02: 03:https://www.cnblogs.com/myx12345/p/116478 ...
- MyBank后感
MyBank系统主要的构成部分有:银行账户的初始化,开户,存款,取款,转账,查询余额,修改密码这些操作. =============================================== ...
- C#版简易RSS阅读器
C#版简易RSS阅读器.由VB版修改完成,感谢aowind的技术支持! 源代码: using System; using System.Drawing; using System.Collection ...
- POJ - 1655 (点分治-树的重心)
题目:https://vjudge.net/contest/307753#problem/D 题意:给你一棵树,让你求出一个点,让他的最大子树的节点数尽量小 思路:最大子树节点数尽量小,一看就是树的重 ...
- Cent OS (一)Cents OS的基本安装
1.实验环境: VMware Workstation Pro 14 Pro Cent OS 7 系列. 2. 镜像地址传送门: 阿里云开源镜像站:http://mirrors.aliyun.com ...
- Classic IPC Problems 经典的进程间通信问题
The Producer-Consumer Problem Presenter Notes: 生产者消费者问题(英语:Producer-consumer problem),也称有限缓冲问题(英语:Bo ...
- Linux 下 VIM 的操作
其实VI 和vim本质上没有多大区别,,但是VIM 可以高亮关键字,使得更受青睐 vim里面有3种模式:命令模式,编辑模式,末行模式 1. vim--->:set number VIM 打开文档 ...
- mongodb在linux 上要注意的一些东西
没有配成开机启动服务,在bin目录下还要使用./mongod去启动,暂时先这样,另外要说的是, child process failed, exited with error number 1说明配置 ...