描述

Given an array nums of integers, we must modify the array in the following way: we choose an i and replace nums[i] with -nums[i], and we repeat this process k times in total. (We may choose the same index i multiple times.)

Return the largest possible sum of the array after modifying it in this way.

Example 1:

Input: nums = [4,2,3], k = 1
Output: 5
Explanation: Choose indices (1,) and nums becomes [4,-2,3].

Example 2:

Input: nums = [3,-1,0,2], k = 3
Output: 6
Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].

Example 3:

Input: nums = [2,-3,-1,5,-4], k = 2
Output: 13
Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].

Note:

1 <= nums.length <= 10000
1 <= k <= 10000
-100 <= nums[i] <= 100

解析

根据题意,就是执行 k 次操作,该操作就是必须将 nums 中的某个数变为相反数,经过 k 次操作之后求 nums 的和。直接循环 k 次,每次循环在 nums 里面找出相反数为最大的元素,将其改为其相反数,经过 k 轮循环,最后对 nums 求和即可。

解答

class Solution(object):def largestSumAfterKNegations(self, nums, k):""":type nums: List[int]:type k: int:rtype: int"""while k:mx = float("-inf")idx = Nonefor i,n in enumerate(nums):if -n>=mx:mx = -nidx = inums[idx] = -nums[idx]k-=1return sum(nums)

运行结果

Runtime: 300 ms, faster than 5.56% of Python online submissions for Maximize Sum Of Array After K Negations.
Memory Usage: 13.5 MB, less than 62.50% of Python online submissions for Maximize Sum Of Array After K Negations.

解析

上面的解法好理解,但是效率不高,所以用数据结构中的堆可以更有效,思路和上面一样,每次从堆 nums 中找出最小值,并将其变为相反数,然后更新当前 nums 的和,重复 k 次即可得到答案。

解答

 class Solution(object):def largestSumAfterKNegations(self, nums, k):""":type nums: List[int]:type k: int:rtype: int"""result = sum(nums)heapq.heapify(nums)while k:mn = heapq.heappop(nums)heapq.heappush(nums, -mn)result += -mn*2k -=1return result

运行结果

Runtime: 44 ms, faster than 34.72% of Python online submissions for Maximize Sum Of Array After K Negations.
Memory Usage: 13.4 MB, less than 62.50% of Python online submissions for Maximize Sum Of Array After K Negations.

原题链接:https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/

您的支持是我最大的动力

leetcode 1005. Maximize Sum Of Array After K Negations(python)相关推荐

  1. leetcode 1005 Maximize Sum Of Array After K Negations

    leetcode 1005 Maximize Sum Of Array After K Negations 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,我们只 ...

  2. LeetCode1005.Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)

    1005.Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和) Description Difficulty: easy Example 1: ...

  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. 1005.E. Maximize Sum Of Array After K Negations

    一个数组每次选一个*(-1),K次后求最大sum. desc 思路: 1. 维护最小堆,either in place or not. 2. 细分析负数数量和K奇偶. K<=#负数:翻转最小的K ...

  6. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  7. leetcode 216. Combination Sum III | 216. 组合总和 III(Java)

    题目 https://leetcode.com/problems/combination-sum-iii/ 题解 回溯法(back tracking)是一种选优搜索法.可以理解为通过选择不同的岔路口寻 ...

  8. 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)

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

  9. leetcode 1662. Check If Two String Arrays are Equivalent(python)

    描述 Given two string arrays word1 and word2, return true if the two arrays represent the same string, ...

最新文章

  1. 【学术相关】专家:硕博研究生阶段还是要发SCI,取消了学生就不干活了
  2. 用ASP.NET Core MVC 和 EF Core 构建Web应用 (一)
  3. MySQL面试准备——64页pdf
  4. ubuntu下解压缩zip,tar,tar.gz和tar.bz2文件
  5. 06.QT菜单栏QAction学习(二)
  6. Python 编程总结
  7. VNC方式连接树莓派
  8. MD5算法是否可逆?
  9. 计算机电路基础知识点总结,《计算机电路基础》课程教学方法的探索
  10. ppt模板如何制作动态时间轴?
  11. 剑指offer题目及答案
  12. 【渝粤题库】国家开放大学2021春2196社会调查研究与方法题目
  13. 从小市值因子策略入手,带你入门量化投资 (附年化收益率77.83%策略)
  14. 2020年数据智能领域的“三宗最”,吴晓波罗胖都没提!
  15. 前端屏幕尺寸和分辨率_屏幕尺寸、分辨率、倍率到底是什么鬼
  16. 搜狗号码通与天翼开放平台达成合作 可识别号码扩容
  17. 精通mysql索引机制,你就不用再背sql优化口诀了!!(万字长文)
  18. 『开发技术』Python中文分词工具SnowNLP教程
  19. 浅析decltype一些有趣(实用)的用法
  20. 如何去掉ppt模板中自带的背景音乐(没有小喇叭)

热门文章

  1. 5天学会Linux C高级
  2. 深度学习第六周 tensorflow实现好莱坞明星识别
  3. 微信公众号广告点击率预估效果优化
  4. fastjson null 值处理
  5. Prometheus(普罗米修斯)架构及原理
  6. 浏览器被“hao123.3377.com”主页劫持的解决办法
  7. 手游服务器维护公告,问道手游服务器维护公告
  8. 网易极客战记计算机科学3攻略,【网易官方】极客战记(codecombat)攻略-地牢-循环又循环...
  9. PHP实现二维数组转字符串
  10. docker安装及启动mysql服务