【陪伴式刷题】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)
相关推荐
忆~遂愿几秒前
ops-cv 算子库深度解析:面向视觉任务的硬件优化与数据布局(NCHW/NHWC)策略
java·大数据·linux·人工智能
小韩学长yyds10 分钟前
Java序列化避坑指南:明确这4种场景,再也不盲目实现Serializable
java·序列化
仟濹11 分钟前
【Java基础】多态 | 打卡day2
java·开发语言
Re.不晚12 分钟前
JAVA进阶之路——无奖问答挑战2
java·开发语言
月挽清风16 分钟前
代码随想录第十五天
数据结构·算法·leetcode
Ro Jace1 小时前
计算机专业基础教材
java·开发语言
mango_mangojuice1 小时前
Linux学习笔记(make/Makefile)1.23
java·linux·前端·笔记·学习
程序员侠客行1 小时前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis
时艰.1 小时前
Java 并发编程 — 并发容器 + CPU 缓存 + Disruptor
java·开发语言·缓存
丶小鱼丶2 小时前
并发编程之【优雅地结束线程的执行】
java