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 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

解法一

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;
    }
}

原文地址:https://www.cnblogs.com/shely-Wangfan/p/10506423.html

时间: 2024-11-08 23:59:22

1005. Maximize Sum Of Array After K Negations的相关文章

leetcode 1005 Maximize Sum Of Array After K Negations &amp; leetcode 1006 Clumsy Factorial

leetcode 1005 Sort the array first. The negation rules are quite simple: execute negation for K times,so use a for loop after negation, if the next number (if has) is smaller, the next number is next to negation (if still in for loop). Here we use a

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

题目标签:Greedy 每一次都找最小的值 进行更改. 可以利用 priority queue 来实现操作. 具体看code. Java Solution: Runtime:  5 ms, faster than 33.84% Memory Usage: 38.6 MB, less than 11.76 % 完成日期:02/26/2020 关键点:priority queue class Solution { public int largestSumAfterKNegations(int[]

[Swift Weekly Contest 127]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 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

Maximize Sum Of Array After K Negations

1 heapq.heapify(A) 2 for i in range(K): 3 heapq.heapreplace(A, -A[0]) 4 5 return sum(A) 最近在看python,该题现在最快的算法 1 A.sort() 2 bZero = False 3 ret = 0; 4 minNum = sys.maxsize 5 for a in A: 6 if a < 0: 7 if K > 0: 8 a = -a 9 K -= 1 10 ret += a 11 elif a =

LeetCode OJ_题解(python):001-Two Sum 【Array】【Easy】

题目: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. 输入一个数组和target,要在一个数组中找到两个数字,其和为t

215. Kth Largest Element in an Array 第K大的数

class Solution { public: int quicksort(vector<int>& nums, int start, int end, int k){ int i = start; int j = end; int x = nums[i]; while (i<j){ //快排核心- while (nums[j]<x && i<j) j--; if (i<j) nums[i++] = nums[j]; while (nums[i

软件工程课堂练习——N层电梯只停一层求乘客爬楼层数最少(基本方法+优化方法)

题目: •石家庄铁道大学基础大楼一共有四部电梯,每层都有人上下,电梯在每层都停.信1201-1班的张一东觉得在每层都停觉得不耐烦. •由于楼层不太高,在上下课高峰期时时,电梯从一层上行,但只允许停在某一楼层.在一楼时,每个乘客选择自己的目的层,电梯则自动计算出应停的楼层. •问电梯停在那一楼层,能够保证这次乘坐电梯的所有乘客爬楼梯的层数之和最少. 一.设计思想 1.解法一 可以从第一层开始枚举一直到第N层,然后计算如果电梯在第x层停的话所有乘客总共要爬多少层楼.这是最为直接的一个解法.程序代码就

Leetcode: Max Sum of Rectangle No Larger Than K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

[LeetCode] K sum(2Sum、3Sum、4Sum)

1 2Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please not