【陪伴式刷题】Day 28|贪心算法|1005.K 次取反后最大化的数组和(Maximize Sum of Array after K Negations)

刷题顺序按照代码随想录建议

题目描述

英文版描述

Given an integer array nums and an integer k, modify the array in the following way:

  • choose an index i and replace nums[i] with -nums[i].

You should apply this process exactly k times. You 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: nums = [4,2,3], k = 1 Output: 5 Explanation: Choose index 1 and nums becomes [4,-2,3].

Example 2:

Input: nums = [3,-1,0,2], k = 3 Output: 6 Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].

Example 3:

Input: nums = [2,-3,-1,5,-4], k = 2 Output: 13 Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].

Constraints:

  • 1 <= nums.length <= 10^4
  • -100 <= nums[i] <= 100
  • 1 <= k <= 10^4

英文版地址

leetcode.com/problems/ma...

中文版描述

给你一个整数数组 nums 和一个整数 k ,按以下方法修改该数组:

  • 选择某个下标 i 并将 nums[i] 替换为 -nums[i]

重复这个过程恰好 k 次。可以多次选择同一个下标 i

以这种方式修改数组后,返回数组 可能的最大和

示例 1:

输入: nums = [4,2,3], k = 1 输出: 5 解释: 选择下标 1 ,nums 变为 [4,-2,3] 。

示例 2:

输入: nums = [3,-1,0,2], k = 3 输出: 6 解释: 选择下标 (1, 2, 2) ,nums 变为 [3,1,0,2] 。

示例 3:

输入: nums = [2,-3,-1,5,-4], k = 2 输出: 13 解释: 选择下标 (1, 4) ,nums 变为 [2,3,-1,5,4] 。

提示:

  • 1 <= nums.length <= 10^4
  • -100 <= nums[i] <= 100
  • 1 <= k <= 10^4

中文版地址

leetcode.cn/problems/ma...

解题方法

俺这版

java 复制代码
class Solution {
    public int largestSumAfterKNegations(int[] nums, int k) {
        Arrays.sort(nums);
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0 && k > 0) {
                nums[i] = -nums[i];
                k--;
            }
            sum = sum + nums[i];
        }
        if (k == 0) {
            return sum;
        }
        if (k > 0) {
            Arrays.sort(nums);
            if (k % 2 == 0) {
                return sum;
            }
            sum = sum-2*nums[0];
        }
        return sum;
    }
}

复杂度分析

  • 时间复杂度:O(nlogn),n为数组长度,主要是排序耗时
  • 空间复杂度:O(nlogn),n为数组长度,整个方法主要占用额外内存的是两次数组排序

官方版

java 复制代码
class Solution {
    public int largestSumAfterKNegations(int[] nums, int K) {
        // 将数组按照绝对值大小从大到小排序,注意要按照绝对值的大小
    nums = IntStream.of(nums)
             .boxed()
             .sorted((o1, o2) -> Math.abs(o2) - Math.abs(o1))
             .mapToInt(Integer::intValue).toArray();
    int len = nums.length;      
    for (int i = 0; i < len; i++) {
        //从前向后遍历,遇到负数将其变为正数,同时K--
        if (nums[i] < 0 && K > 0) {
            nums[i] = -nums[i];
            K--;
        }
    }
    // 如果K还大于0,那么反复转变数值最小的元素,将K用完
    if (K % 2 == 1) nums[len - 1] = -nums[len - 1];
    return Arrays.stream(nums).sum();

    }
}

复杂度分析

  • 时间复杂度:O(nlogn),n为数组长度,主要是排序耗时
  • 空间复杂度:O(1)
相关推荐
岁忧44 分钟前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
CJi0NG1 小时前
【自用】JavaSE--算法、正则表达式、异常
java
Hellyc1 小时前
用户查询优惠券之缓存击穿
java·redis·缓存
今天又在摸鱼2 小时前
Maven
java·maven
老马啸西风2 小时前
maven 发布到中央仓库常用脚本-02
java·maven
代码的余温2 小时前
MyBatis集成Logback日志全攻略
java·tomcat·mybatis·logback
Maybyy3 小时前
力扣61.旋转链表
算法·leetcode·链表
一只叫煤球的猫3 小时前
【🤣离谱整活】我写了一篇程序员掉进 Java 异世界的短篇小说
java·后端·程序员
斐波娜娜3 小时前
Maven详解
java·开发语言·maven