【陪伴式刷题】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)
相关推荐
参.商.13 分钟前
【Day 53】76. 最小覆盖子串
leetcode·golang
SL_staff32 分钟前
别急着买商业规则引擎:90%风控场景根本不需要全量编码能力
java·程序员·groovy
ManageEngineITSM1 小时前
DevOps和ITIL是什么关系?是替代还是互补一文讲清
java·服务器·资产管理·变更管理
SL_staff1 小时前
ERP排程总在纸上谈兵?JVS-APS如何用真实产能约束打通计划与执行闭环
java·人工智能·开源
xcl09252 小时前
24小时自助健身房系统软件开发实战指南:从架构到部署
java·spring boot
码少女2 小时前
Linux--多路转接之select
java·服务器·数据库
虚无的纽扣3 小时前
【力扣刷题】第二天:无重复字符的最长字串、移动零问题
算法·leetcode·排序算法
nvvas3 小时前
Java 入门(四):数组——数据的集合管理
java·开发语言
Lambert2813 小时前
AgentScope Java 从零(07):官方有权限引擎,我却在工具里写了个 if
java·后端·ai编程
Kyrie_kk4 小时前
Java--ThreadLocal线程副本(一)
java·后端