LeetCode #1005 Maximize Sum Of Array After K Negations K 次取反后最大化的數(shù)組和

1005 Maximize Sum Of Array After K Negations K 次取反后最大化的數(shù)組和

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

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 <= A.length <= 10000
1 <= K <= 10000
-100 <= A[i] <= 100

題目描述:
給定一個(gè)整數(shù)數(shù)組 A,我們只能用以下方法修改該數(shù)組:我們選擇某個(gè)個(gè)索引 i 并將 A[i] 替換為 -A[i],然后總共重復(fù)這個(gè)過(guò)程 K 次。(我們可以多次選擇同一個(gè)索引 i。)

以這種方式修改數(shù)組后,返回?cái)?shù)組可能的最大和。

示例 :

示例 1:

輸入:A = [4,2,3], K = 1
輸出:5
解釋:選擇索引 (1,) ,然后 A 變?yōu)?[4,-2,3]。

示例 2:

輸入:A = [3,-1,0,2], K = 3
輸出:6
解釋:選擇索引 (1, 2, 2) ,然后 A 變?yōu)?[3,1,0,2]。

示例 3:

輸入:A = [2,-3,-1,5,-4], K = 2
輸出:13
解釋:選擇索引 (1, 4) ,然后 A 變?yōu)?[2,3,-1,5,4]。

提示:

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

思路:

  1. 每次取最小的負(fù)數(shù)(絕對(duì)值最大的負(fù)數(shù))反轉(zhuǎn), 如果數(shù)組中全是正數(shù), 取最小的正數(shù)反轉(zhuǎn)即可
    由于數(shù)組中的元素的絕對(duì)值均小于等于100, 可以將數(shù)組中的元素映射到一個(gè)長(zhǎng)度為 201的數(shù)組中
    時(shí)間復(fù)雜度O(kn), 空間復(fù)雜度O(1)
  2. 每次先排序, 然后將最小的數(shù)取反
    時(shí)間復(fù)雜度O(knlgn), 空間復(fù)雜度O(1)

代碼:
C++:

class Solution 
{
public:
    int largestSumAfterKNegations(vector<int>& A, int K) 
    {
        int num[201]{0};
        for (auto a : A) ++num[a + 100];
        int i = 0, result = 0;
        while (K--) 
        {
            while (!num[i]) ++i;
            --num[i];
            ++num[200 - i];
            if (i > 100) i = 200 - i;
        }
        for (; i < 201; i++) result += (i - 100) * num[i];
        return result;
    }
};

Java:

class Solution {
    public int largestSumAfterKNegations(int[] A, int K) {
        int num[] = new int[201];
        for (int a : A) num[a + 100]++;
        int i = 0, result = 0;
        while (K-- > 0) {
            while (num[i] == 0) i++;
            num[i]--;
            num[200 - i]++;
            if (i > 100) i = 200 - i;
        }
        for (; i < 201; i++) result += (i - 100) * num[i];
        return result;
    }
}

Python:

class Solution:
    def largestSumAfterKNegations(self, A: List[int], K: int) -> int:
        for _ in range(K):
            A.sort()
            A[0] = -A[0]
        return sum(A)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容