博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1005. Maximize Sum Of Array After K Negations
阅读量:5084 次
发布时间:2019-06-13

本文共 2973 字,大约阅读时间需要 9 分钟。

题目

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: 6Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2].

Example 3:

Input: A = [2,-3,-1,5,-4], K = 2Output: 13Explanation: 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

解法一

class Solution {    public int largestSumAfterKNegations(int[] A, int K) {       java.util.Arrays.sort(A);        int lenA = A.length;        int lenB=0,lenC=0;        int result;        if(A[0]>=0){            result = arraySum(A,1,lenA-1);            if(K%2==0)               result+=A[0];            else               result-=A[0];        }else if(A[lenA-1]<=0){            result = arraySum(A,K,lenA-1)-arraySum(A,0,1);        }else{            result=largestSumInBothPlusMinus(A,K);        }               return result;    }    public int largestSumInBothPlusMinus(int[] A, int K) {        int result=0;        int lenA = A.length;         int start = 0;        int end = A.length-1;        int mid=(start+end)/2;        int maxLessIndex=-1;        while(mid>0&&mid+1
=0){ end=mid-1; mid=(start+mid-1)/2; if(mid==0){ maxLessIndex=mid; break; } }else{ maxLessIndex=mid-1; break; } } } if(K

解法二:简化求最大的小于0的值得坐标

class Solution {    public int largestSumAfterKNegations(int[] A, int K) {       java.util.Arrays.sort(A);        int lenA = A.length;        int lenB=0,lenC=0;        int result;        if(A[0]>=0){            result = arraySum(A,1,lenA-1);            if(K%2==0)               result+=A[0];            else               result-=A[0];        }else if(A[lenA-1]<=0){            result = arraySum(A,K,lenA-1)-arraySum(A,0,1);        }else{            result=largestSumInBothPlusMinus(A,K);        }               return result;    }    public int largestSumInBothPlusMinus(int[] A, int K) {        int maxLessIndex=-1;        int result=0;        int lenA=A.length;        int i;        for(i=0;i

解法三:

class Solution {    public int largestSumAfterKNegations(int[] A, int K) {       java.util.Arrays.sort(A);       int count=0;       int sum=0;       int lenA=A.length;       int minNum=A[lenA-1];       for(int i=0;i
K || (K-count)%2==0){ return sum; }else{ return sum-2*minNum; } }}

注意:if(A[i]<0 && ++count<=K)这里别写成了++count

转载于:https://www.cnblogs.com/shely-Wangfan/p/10506423.html

你可能感兴趣的文章
web聊天相关知识
查看>>
PAT_1018 锤子剪刀布
查看>>
xmlhttp的OnReadyStateChange事件
查看>>
python连接oracle数据库
查看>>
C++异常处理
查看>>
捕获键盘和鼠标的消息机制
查看>>
Csharp 简单操作Word模板文件
查看>>
laravel 配置设置
查看>>
常用linux命令
查看>>
git 代码更新
查看>>
PO BO VO DTO POJO DAO概念及其作用
查看>>
IntelliJ IDEA 把Json字符串 增加到IDE里 用windows记事本 能自动转换(自动增加转义字符)...
查看>>
eclipse转到idea过程中的基本设置...
查看>>
【Chrome】离线版下载
查看>>
sql参数化查询语句
查看>>
python操作日期和时间的方法
查看>>
我们应该怎么去理解.net类型传递 .
查看>>
[Leetcode] Next Permutation
查看>>
类加载器实战剖析与疑难点解析
查看>>
在NodeJS中配置aws ec2
查看>>