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 ...
随机推荐
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- POJ - 2226 Muddy Fields (最小顶点覆盖)
*.*. .*** ***. ..*. 题意:有一个N*M的像素图,现在问最少能用几块1*k的木条覆盖所有的 * 点,k为>=1的任意值. 分析:和小行星那题很像.小行星那题是将一整行(列)看作 ...
- HYSBZ - 2243 染色 (树链剖分+线段树)
题意:树上每个结点有自己的颜色,支持两种操作:1.将u到v路径上的点颜色修改为c; 2.求u到v路径上有多少段不同的颜色. 分析:树剖之后用线段树维护区间颜色段数.区间查询区间修改.线段树结点中维护的 ...
- mysql 5.7快速部署
目录 一:官网下载mysql二级制包.... 1 二:mysql二级制包解压.... 1 三:设置mysql库文件路径与授权... 1 四. 创建配置文件... 2 五:数据库初始化... 5 六: ...
- MySQL-checkpoint技术
几个知识点: 缓冲池:缓存磁盘数据,通过内存速度弥补CPU速度和磁盘速度的鸿沟. 脏页:LRU列表中被修改的页,和磁盘上的数据不一致 刷新频率:每次有脏页就刷新,开销很大.需要一种刷新机制 数据丢失: ...
- 20145219 《Java程序设计》第04周学习总结
20145219 <Java程序设计>第04周学习总结 教材学习内容总结 继承 继承就是避免多个类间重复定义共同行为. 面向对象中,子类继承父类,就是把程序中相同的代码部分提升为父类. R ...
- 20145201 《Java程序设计》第二周学习总结
20145201 <Java程序设计>第二周学习总结 教材学习内容总结 本周学习了课本第三章内容,即JAVA基础语法. 3.1 类型.变量与运算符 基本类型:在java中基本类型主要可区分 ...
- 解决 SMTP Error: Could not connect to SMTP host. 问题
我在使用PHPmailer发邮件时候,遇到了这个问题“SMTP Error: Could not connect to SMTP host.”,分享一下解决方法. 这个错误是PHP版本7产生的.如果我 ...
- 《Pro Git》第2章 Git基础
1.获取Git仓库 1.1从现有的目录中初始化仓库 进入项目目录,git init,会创建一个名为.git的子目录 1.2克隆现有的仓库 git clone [url],会将远程Git仓库中的每一个文 ...
- Linux统计文件数目
统计当前目录下文件数目 $ find . -type f | wc -l 统计文件行数 $ wc -l filename 仅统计内容为pattern的行(只有pattern) $ grep -w &q ...