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[] A, int K) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
        int sum = 0;

        for(int x: A) {
            pq.add(x);
        }

        while( K--  > 0) {
          pq.add(-pq.poll());
        } 

        for(int i = 0; i < A.length; i++) {
            sum += pq.poll();
        }

        return sum;
    }
}

参考资料:LeetCode Discuss

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/12440790.html

时间: 2024-07-31 21:27:57

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

Leetcode 1005. K 次取反后最大化的数组和

1005. K 次取反后最大化的数组和 显示英文描述 我的提交返回竞赛 用户通过次数377 用户尝试次数413 通过次数385 提交次数986 题目难度Easy 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次.(我们可以多次选择同一个索引 i.) 以这种方式修改数组后,返回数组可能的最大和. 示例 1: 输入:A = [4,2,3], K = 1 输出:5 解释:选择索引 (1,) ,然后 A 变为 [

leetcode K 次取反后最大化的数组和

K 次取反后最大化的数组和 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次.(我们可以多次选择同一个索引 i.) 以这种方式修改数组后,返回数组可能的最大和. 示例 1: 输入:A = [4,2,3], K = 1 输出:5 解释:选择索引 (1,) ,然后 A 变为 [4,-2,3]. 示例 2: 输入:A = [3,-1,0,2], K = 3 输出:6 解释:选择索引 (1, 2, 2) ,然后

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

[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

1005.K 次取反后最大化的数组和

这道题我的做法是进行排序 从小到大 取以一个数取反 再进行排序取反 每一次取的数都是最小的 如-10 变 10 排序 取第一个数 class Solution { public int largestSumAfterKNegations(int[] A, int K) { Arrays.sort(A); int sum = 0; for(int i = 0;i < K ;i++) { A[0] = -A[0]; Arrays.sort(A); } for(int i = 0 ; i < A.l

LeetCode1005 K次取反后最大化的数组和(贪心+Java简单排序)

题目: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次.(我们可以多次选择同一个索引 i.) 以这种方式修改数组后,返回数组可能的最大和. 示例 1: 输入:A = [4,2,3], K = 1输出:5解释:选择索引 (1,) ,然后 A 变为 [4,-2,3]. 示例 2: 输入:A = [3,-1,0,2], K = 3输出:6解释:选择索引 (1, 2, 2) ,然后 A 变为 [3,1,0,2]

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

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 560. Subarray Sum Equals K]

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers