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的更多相关文章

  1. 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  2. 【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 ...

  3. 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)

    这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...

  4. LeetCode.1005-K次取负数组和最大(Maximize Sum Of Array After K Negations)

    这是悦乐书的第376次更新,第403篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第237题(顺位题号是1005).给定一个整数数组A,我们必须按以下方式修改数组:我们选 ...

  5. [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 ...

  6. [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 ...

  7. 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题是存储的当前累加和的 ...

  8. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  9. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

随机推荐

  1. mysql 练习题(Day44)

    init.sql文件内容 /* 数据导入: Navicat Premium Data Transfer Source Server : localhost Source Server Type : M ...

  2. go——切片

    切片(slice)可以看作一种对数组的包装形式,它包装的数组为该切片的底层数组.反过来讲,切片是针对其底层数组中某个连续片段的描述,下面的代码声明了一个切片类型的变量: var ips = []str ...

  3. Firebug入门指南(转)

    本文转自:http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 作者: 阮一峰 日期: 2008年6月 8日 据说,对于网页开发人员 ...

  4. Atom常用快捷键及设置

    常用快捷键: cmd-z 修改历史中后退 cmd-y 修改历史中前进 ctl-shift-c 复制该聚焦文件的路径 cmd-/ 注释 shift-cmd-d 复制整行并粘贴到下一行 ctl-shift ...

  5. SVN使用—常用命令及避免冲突的方法

    一.SVN启动 [root@localhost ~]# mkdir /data/svn [root@localhost ~]# svnadmin create /data/svn/test [root ...

  6. Mac & Xcode 技巧

    1.Xcode 删除过期的Provisioning Profile文件 解决:Xcode 中所有的Provisioning Profile文件路径,都在 ~/Library/MobileDevice/ ...

  7. iOS动画进阶 - 手摸手教你写 Slack 的 Loading 动画

    如果移动端访问不佳,可以访问我的个人博客 前几天看了一篇关于动画的博客叫手摸手教你写 Slack 的 Loading 动画,看着挺炫,但是是安卓版的,寻思的着仿造着写一篇iOS版的,下面是我写这个动画 ...

  8. centos 6的LAMP一键安装包(可选择/升级版本)

    安装步骤 事前准备(安装 wget.screen.unzip,创建 screen 会话) yum -y install wget screen git git clone 并赋予脚本执行权限 git ...

  9. sem学习

    关键字的选取搜索引擎营销的关键字选取是非常重要的一步,合适的关键字可以为企业带来可观的咨询量.关键字一般分4种,品牌词,产品词,行业词,竞品词,对于这四类词不同的推广策略有着不同的侧重点,根据市场情况 ...

  10. spark集群安装

    cd到conf中去 修改名字   mv spark-env.sh.template spark-env.sh 并且修改内容 添加: export JAVA_HOME=/root/java/jdk1.8 ...