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 ...
随机推荐
- c#中类和成员的修饰符介绍
类访问修饰符: public 访问级别最高,公共访问没有限制. internal 只允许在本程序集内访问,其他程序集或站点引用其所在的程序集无法访问此类. 例如程序集LibraryA写有ClassA, ...
- html-3,table 表格标签 tr th td caption thead tbody tfoot 的简单使用
<!-- table border='1' style="border-collapse:collapse;" border 表格的像素宽度 border-collapse: ...
- cdoj1339郭大侠与线上游戏
地址:http://acm.uestc.edu.cn/#/problem/show/1339 题目: 郭大侠与线上游戏 Time Limit: 6000/2000MS (Java/Others) ...
- js 的一些兼容性写法
①添加事件方法 addHandler:function(element,type,handler){ if(element.addEventListener){//检测是否为DOM2级方法 ...
- Java基础_基本语法
Java基本语法 一:关键字 在Java中有特殊含义的单词(50). 二:标志符 类名,函数名,变量名的名字的统称. 命名规则: 可以是字母,数字,下划线,$. 不能以数字开头. 见名之意. 驼峰规则 ...
- 2018年Java面试题搜集
2018年Java面试题搜集 一.Servlet执行流程(浏览器访问servlet的过程容器) 客户端发起http请求,web服务器将请求发送到servlet容器,servlet容器解析url并根据w ...
- 阻塞方法与InterruptedException
什么是阻塞方法?为什么会抛出InterruptedException? 一般方法的完成只取决于它所要做的事情,以及是否有足够多可用的计算资源(CPU 周期和内存). 而阻塞方法的完成还取决于一些外部的 ...
- CreateWindow创建无边框 可拉伸窗体
createwindow 定义 HWND WINAPI CreateWindow( _In_opt_ LPCTSTR lpClassName, _In_opt_ LPCTSTR lpWindowNam ...
- sql 加密解密函数
if object_ID ( 'fn_ACITEncryption' ) is not null drop function fn_ACITEncryption go create ...
- Apache HTTP Server——官网下载
Windows版 Apache 2.4.x OpenSSL 1.0.2 VC14 ——Apache 2.4.34 x64(注:x64就是64位,x86就是32位) https://www.apach ...