【陪伴式刷题】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)
相关推荐
敢敢变成了憨憨2 小时前
java操作服务器文件(把解析过的文件迁移到历史文件夹地下)
java·服务器·python
苇柠2 小时前
Java补充(Java8新特性)(和IO都很重要)
java·开发语言·windows
Lin_XXiang2 小时前
java对接bacnet ip协议(跨网段方式)
java·物联网
编程绿豆侠2 小时前
力扣HOT100之多维动态规划:62. 不同路径
算法·leetcode·动态规划
白总Server2 小时前
C++语法架构解说
java·网络·c++·网络协议·架构·golang·scala
咖啡啡不加糖2 小时前
雪花算法:分布式ID生成的优雅解决方案
java·分布式·后端
小杜-coding3 小时前
天机学堂(初始项目)
java·linux·运维·服务器·spring boot·spring·spring cloud
钢铁男儿3 小时前
深入剖析C#构造函数执行:基类调用、初始化顺序与访问控制
java·数据库·c#
小鹭同学_3 小时前
Java基础 Day27
java·开发语言
EdmundXjs3 小时前
IO Vs NIO
java·开发语言·nio