题目如下:

Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i]with -A[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: A = [4,2,3], K = 1
Output: 5
Explanation: Choose indices (1,) and A becomes [4,-2,3].

Example 2:

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

Example 3:

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

Note:

  1. 1 <= A.length <= 10000
  2. 1 <= K <= 10000
  3. -100 <= A[i] <= 100

解题思路:把A按升序排序,如果存在负数,那么优先把较大的负数变成正数,直到K次变换用换为止。如果K大于负数的个数,这时A中已经全部是正数了,我们知道对一个数变换两次相当于不做变换,因此只要判断剩余的可变换次数是奇数还是偶数。如果是偶数,那表示不用再做变换;如果是奇数,则把A中最小的正数做变换。最后求出A的总和即可。

代码如下:

class Solution(object):
def largestSumAfterKNegations(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
A.sort()
res = 0
lastMin = 10001
for i in A:
if K == 0:
res += i
elif i < 0:
res += (-i)
K -= 1
lastMin = min(lastMin,-i)
else:
K = K%2
if K == 1:
if lastMin < i:
res -= 2*lastMin
res += i
else:
res -= i
K = 0
else:
res += i
return res

【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

    class Solution(object): def largestSumAfterKNegations(self, A, K): """ :type A: List[ ...

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

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

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

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

  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】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  7. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  8. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

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

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

随机推荐

  1. vue项目放在IE上页面空白的问题

    Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码 1.npm install babel-polyfill --save 2.main.js中引入 import 'babel-poly ...

  2. nuget cli 打包发包

    微软官网打包说明:https://docs.microsoft.com/zh-cn/nuget/quickstart/create-and-publish-a-package-using-visual ...

  3. C++ 常用数学运算(加减乘除)代码实现 Utils.h, Utils.cpp(有疑问欢迎留言)

    Utils.h #pragma once class Utils { public: static double* array_diff(double*A,double B[],int n); sta ...

  4. C++ 彩色图像(RGB)三通道直方图计算和绘制,图像逆时针旋转90° 实现代码

    #include "iostream" #include "opencv2/opencv.hpp" #include "vector" us ...

  5. 4412 搭建和测试NFS服务器

    一.NFS网络文件系统 NFS是Network FileSystem的缩写,NFS是基于UDP/IP协议的应用.它的最大功能就是可以通过网络让不同的机器,不通的操作系统彼此共享文件, 可以通过NFS挂 ...

  6. linux 文件及目录结构体系

    linux 目录的特点: 1). /是所有目录的顶点 2).目录结构像一颗倒挂的树 3).目录和磁盘分区是没有关联的 4)./下不同的目录可能对应不同的分区或磁盘 5).所有的目录都是按照一定的类别有 ...

  7. [CSP-S模拟测试]:biology(DP)

    题目传送门(内部题23) 输入格式 第一行有$2$个整数$n,m$.接下来有$n$行,每行$m$个整数,表示$a$数组.接下来有$n$行,每行$m$个整数,表示$b$数组. 输出格式 一行一个整数表示 ...

  8. [CSP-S模拟测试]:阴阳(容斥+计数+递推)

    题目传送门(内部题16) 输入格式 第一行两个整数$n$和$m$,代表网格的大小.接下来$n$行每行一个长度为$m$的字符串,每个字符若为$W$代表这个格子必须为阳,若为$B$代表必须为阴,若为$?$ ...

  9. vector的自定义实现

    #pragma warning(disable:4996) #include<iostream> #include<string> #include<vector> ...

  10. 抓包工具fiddler下载配置(一):下载/安装&信任证书

    简介 Fiddler一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html,js,css等文件 ...