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 ...
随机推荐
- IIS Internet Information Service
Visual Studio 和 visio 都有的Web服务,IIS 发布的时候,直接可以用本机的IIS进行发布,Windos自带有Web服务,只需要配置一下,然后配上域名就OK了,简直太方便了 来自 ...
- JavaScript 函数,math对象,Date对象 序列化 总结
函数 函数定义 // 普通函数定义 function f1() { console.log("Hello world!"); } // 带参数的函数 function f2(a, ...
- python 对象和类
python中所有数据都是以对象形式存在.对象既包含数据(变量),也包含代码(函数),是某一类具体事物的特殊实例. 面向对象的三大特性为封装.继承和多态. 1.定义类 #定义空类 class Pers ...
- Android 6.0 Kotlin 蓝牙BLE扫描(改为指定时间没有发现新设备后停止扫描使用interface)
package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...
- BKDRHash函数
unsigned int BKDRHash(char*str) { unsigned ;// 31 131 1313 13131 131313 etc.. unsigned ; while(*str) ...
- HDU - 6386 Age of Moyu 2018 Multi-University Training Contest 7 (Dijkstra变型)
题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1:改变至不同编号的路径,花费加1,无论这个编号之前是否走过. 分析:记录每个点的最小花费,再用set维 ...
- unity,如何手动或者使用代码更换材质
在unity中,我们可能需要更换怪物的贴图,来达到以下效果 方法1:手动配置 找到自己配置好的扩展名为.mat的文件,在怪物的节点找到带Mesh Render的子元素,将其拖放到如图位置 方法2:代码 ...
- Unity,android和IOS 防止八门神器注入
八门神器主要是不断筛选,来获取关键属性(比如金币)在内存中的地址,再根据该地址来修改指向的数据就可以成功. 因此,我们需要在金币读取和设置的时候,使用一个偏移量,来达到干扰的目的就可以了 未经仔细测试 ...
- ASYNCAPI
https://www.asyncapi.com Introduction AsyncAPI provides a specification that allows you to define Me ...
- Hadoop2.7.x中所有的DataNode都启动不了解决办法
参考:Hadoop集群所有的DataNode都启动不了解决办法说明现象:我自己出现这个问题的原因是:自己在namenode格式化之后创建了一些文件,然后重新执行了namenode格式化导致的. 现象就 ...