Leetcode 1005. Maximize Sum Of Array After K Negations
class Solution(object):
def largestSumAfterKNegations(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
have_zero = False
negative = []
positive = []
for a in A:
if a < 0:
negative.append(a)
elif a == 0:
have_zero = True
else:
positive.append(a)
neg_size = len(negative)
if neg_size > 0:
negative.sort()
for i in range(neg_size):
if K > 0:
negative[i] = -negative[i]
K -= 1
if (K > 0) and (K % 2 == 1) and (not have_zero):
positive.sort()
if len(positive) > 0:
sub = min(positive[0], negative[neg_size - 1])
else:
sub = negative[neg_size - 1]
return sum(negative) + sum(positive) - 2 * sub
return sum(negative) + sum(positive)
elif have_zero:
return sum(positive)
else:
K = K % 2
if K == 1:
positive.sort()
positive[0] = -positive[0]
return sum(positive)
else:
return sum(positive)
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
题目如下: Given an array A of integers, we must modify the array in the following way: we choose an i an ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
- LeetCode.1005-K次取负数组和最大(Maximize Sum Of Array After K Negations)
这是悦乐书的第376次更新,第403篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第237题(顺位题号是1005).给定一个整数数组A,我们必须按以下方式修改数组:我们选 ...
- [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] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
随机推荐
- Linux知识点总结
- NGUI如何使用汉字
1:准备好字体文件,就是ttf后缀名的那些.. 2:在第一个红线部分,将下拉框选择为Unity,在后面的字体里面选择第一步准备好的字体. 3:创建UILabel,widget里面的Color才是字体的 ...
- C/C++中0xcccccccc...
* 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after a ...
- Kali更新源,亲测目前可用的源
kali更新的时候老是无法定位软件包,网络上大部分中科大.阿里云kali源都不可用,都千篇一律,最后找了这个,网易的,还不错,贴出来大家看看: # 源 deb http://mirrors.163.c ...
- 关于全志A20的Ubuntu12.04 64位系统下环境配置及编译过程笔记【转】
本文转载自:https://blog.csdn.net/buqingbuyuan/article/details/43370199 在安装Ubuntu系统之后,安装编译所需的GCC等工具,一般选用GC ...
- Linux内核中的信号机制--一个简单的例子【转】
本文转载自:http://blog.csdn.net/ce123_zhouwei/article/details/8562958 Linux内核中的信号机制--一个简单的例子 Author:ce123 ...
- JUnit4 入门笔记
Test注解的两个可选参数 expected timeout The Test annotation supports two optional parameters. The first, expe ...
- MyCat分片集群
数据库集群会产生的问题: 自增ID问题 数据关联查询问题(水平拆分) 数据同步问题 数据库集群 自动增长id产生重复的话,解决: UUID形式 (没有排序 不是自增) 设置数据库步长 其他方案: r ...
- HttpStatusCode
https://docs.microsoft.com/en-us/dotnet/api/system.net.httpstatuscode?view=netframework-4.7.2 422 Un ...
- Dispatcher initialization failedUnable to load configuration 解决办法
检查<package name="action" extends="struts-default"></package>中是否有exte ...