【陪伴式刷题】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)
相关推荐
雨辰AI5 分钟前
生产级实测:SpringBoot3 + 达梦数据库接口从 200ms 优化至 20ms 完整调优指南
java·数据库·spring boot·后端·政务
圣保罗的大教堂21 分钟前
leetcode 2130. 链表最大孪生和 中等
leetcode
(Charon)34 分钟前
【C++ 面试高频:内存管理、RAII 和智能指针详解】
java·开发语言·word
凡人叶枫44 分钟前
Effective C++ 条款39:明智而审慎地使用 private 继承
java·数据库·c++·嵌入式开发
轻刀快马1 小时前
跨越软硬件的共鸣(二):从 Cache 写策略看 Redis 与 DB 的一致性博弈
java·开发语言·redis·计算机组成原理
折哥的程序人生 · 物流技术专研1 小时前
Java 23 种设计模式:从踩坑到精通 | 装饰器模式 —— 比继承更灵活的扩展方式,你用过吗?
java·装饰器模式·java面试·结构型模式·java设计模式·javaio·从踩坑到精通
blxr_1 小时前
力扣hot100路径总和Ⅲhttps://leetcode.cn/problems/path-sum-iii/
算法·leetcode·职场和发展
lili00121 小时前
2026 企业 AI 选型新范式:OpenRouter Fusion 证明多模型融合性价比远超单模型,企业该如何重构技术栈? - 微元算力(weytoken)
java·人工智能·python·重构·ai编程
shushangyun_1 小时前
汽车服务行业B2B平台+AI解决方案哪家专业:2026年最新测评
java·运维·网络·数据库·人工智能·汽车
A.说学逗唱的Coke1 小时前
【大模型专题】Spring AI Alibaba × Skill 整合实战:让 AI 真正“会干活
java·人工智能·spring