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.)java
Return the largest possible sum of the array after modifying it in this way.this
Example 1:code
Input: A = [4,2,3], K = 1 Output: 5 Explanation: Choose indices (1,) and A becomes [4,-2,3].
Example 2:ip
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:leetcode
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:rem
原題連接get
解法一it
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<lenA){ if(A[mid]<0){ if(A[mid+1]<0){ start=mid+1; mid=(mid+1+end)/2; if(mid==lenA-2){ maxLessIndex=mid; break; } }else{ maxLessIndex=mid; break; } } else{ if(A[mid-1]>=0){ end=mid-1; mid=(start+mid-1)/2; if(mid==0){ maxLessIndex=mid; break; } }else{ maxLessIndex=mid-1; break; } } } if(K<maxLessIndex+1){ result=-arraySum(A,0,K-1)+arraySum(A,K,maxLessIndex)+arraySum(A,maxLessIndex+1,lenA-1); }else if(K==maxLessIndex+1){ result=-arraySum(A,0,maxLessIndex)+arraySum(A,maxLessIndex+1,lenA-1); }else{ result=-arraySum(A,0,maxLessIndex)+arraySum(A,maxLessIndex+2,lenA-1); int remainK = K-maxLessIndex-1; if(remainK%2==0){ result+=A[maxLessIndex+1]; } else{ if(-A[maxLessIndex]<A[maxLessIndex+1]){ result=result+A[maxLessIndex+1]+2*A[maxLessIndex]; }else{ result-=A[maxLessIndex+1]; } } } return result; } public int arraySum(int [] A, int start, int end){ int sum = 0; for(int i=start;i<=end;i++){ sum+=A[i]; } return sum; } }
解法二:簡化求最大的小於0的值得座標io
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<lenA;i++){ if(A[i]<0) continue; else break; } maxLessIndex=i-1; if(K<maxLessIndex+1){ result=-arraySum(A,0,K-1)+arraySum(A,K,maxLessIndex)+arraySum(A,maxLessIndex+1,lenA-1); }else if(K==maxLessIndex+1){ result=-arraySum(A,0,maxLessIndex)+arraySum(A,maxLessIndex+1,lenA-1); }else{ result=-arraySum(A,0,maxLessIndex)+arraySum(A,maxLessIndex+2,lenA-1); int remainK = K-maxLessIndex-1; if(remainK%2==0){ result+=A[maxLessIndex+1]; } else{ if(-A[maxLessIndex]<A[maxLessIndex+1]){ result=result+A[maxLessIndex+1]+2*A[maxLessIndex]; }else{ result-=A[maxLessIndex+1]; } } } return result; } public int arraySum(int [] A, int start, int end){ int sum = 0; for(int i=start;i<=end;i++){ sum+=A[i]; } return sum; } }
解法三:class
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<lenA;i++){ if(A[i]<0 && ++count<=K){ A[i]=-A[i]; } minNum=Math.min(A[i],minNum); sum+=A[i]; } if(count>K || (K-count)%2==0){ return sum; }else{ return sum-2*minNum; } } }
注意:if(A[i]<0 && ++count<=K)這裏別寫成了++count