leetcode K 次取反后最大化的数组和

K 次取反后最大化的数组和

给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次。(我们可以多次选择同一个索引 i。)

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

示例 1:

输入:A = [4,2,3], K = 1
输出:5
解释:选择索引 (1,) ,然后 A 变为 [4,-2,3]。

示例 2:

输入:A = [3,-1,0,2], K = 3
输出:6
解释:选择索引 (1, 2, 2) ,然后 A 变为 [3,1,0,2]。

示例 3:

输入:A = [2,-3,-1,5,-4], K = 2
输出:13
解释:选择索引 (1, 4) ,然后 A 变为 [2,3,-1,5,4]。

提示:

  1. 1 <= A.length <= 10000
  2. 1 <= K <= 10000
  3. -100 <= A[i] <= 100

解题思路 这道题使用的是贪心算法,根据题意我们可以知道要求该数组最大的和,每次翻转都要翻使和最大,首先我们先通过排序来把让其从小到大排列,最小的是负数情况下吧负数最小的翻转可以获得最大和,当负数没有了时在排一遍,把最小的进行翻转即可。还要注意翻转次数,偶数次可以翻转同一个数。

 1 class Solution {
 2     public int largestSumAfterKNegations(int[] A, int K) {
 3         int sz[]=new int[A.length];
 4         int fs=0;
 5         for (int i = 0; i < A.length; i++) {
 6             sz[i]=A[i];
 7             if(A[i]<0){
 8                 fs++;
 9             }
10         }
11         Arrays.sort(sz);
12         int ks=K;
13         int sum=0;
14         if(fs>ks) {
15             for (int i = 0; i < sz.length; i++) {
16                 if (ks != 0) {
17                     sz[i] = -sz[i];
18                     ks--;
19                 }
20                 sum =sum+sz[i];
21             }
22             return sum;
23         }
24         else{
25             for (int i = 0; i < sz.length; i++) {
26                 if(fs!=0){
27                     sz[i]=-sz[i];
28                     fs--;
29                     ks--;
30                 }
31                 sum=sum+sz[i];
32             }
33             if(ks!=0){
34                 if(ks%2==0){
35                     return sum;
36                 }
37                 else{
38                     Arrays.sort(sz);
39                     return sum-sz[0]*2;
40                 }
41             }
42             else{
43                 return sum;
44             }
45         }
46
47
48     }
49 }

原文地址:https://www.cnblogs.com/hwhWorld/p/10511204.html

时间: 2024-07-31 21:28:00

leetcode K 次取反后最大化的数组和的相关文章

Leetcode 1005. K 次取反后最大化的数组和

1005. K 次取反后最大化的数组和 显示英文描述 我的提交返回竞赛 用户通过次数377 用户尝试次数413 通过次数385 提交次数986 题目难度Easy 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次.(我们可以多次选择同一个索引 i.) 以这种方式修改数组后,返回数组可能的最大和. 示例 1: 输入:A = [4,2,3], K = 1 输出:5 解释:选择索引 (1,) ,然后 A 变为 [

1005.K 次取反后最大化的数组和

这道题我的做法是进行排序 从小到大 取以一个数取反 再进行排序取反 每一次取的数都是最小的 如-10 变 10 排序 取第一个数 class Solution { public int largestSumAfterKNegations(int[] A, int K) { Arrays.sort(A); int sum = 0; for(int i = 0;i < K ;i++) { A[0] = -A[0]; Arrays.sort(A); } for(int i = 0 ; i < A.l

LeetCode1005 K次取反后最大化的数组和(贪心+Java简单排序)

题目: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次.(我们可以多次选择同一个索引 i.) 以这种方式修改数组后,返回数组可能的最大和. 示例 1: 输入:A = [4,2,3], K = 1输出:5解释:选择索引 (1,) ,然后 A 变为 [4,-2,3]. 示例 2: 输入:A = [3,-1,0,2], K = 3输出:6解释:选择索引 (1, 2, 2) ,然后 A 变为 [3,1,0,2]

[Swift Weekly Contest 127]LeetCode1005. K 次取反后最大化的数组和 | Maximize Sum Of Array After K Negations

Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total.  (We may choose the same index i multiple times.) Return the largest possible sum of

LeetCode 1005. Maximize Sum Of Array After K Negations (K 次取反后最大化的数组和)

题目标签:Greedy 每一次都找最小的值 进行更改. 可以利用 priority queue 来实现操作. 具体看code. Java Solution: Runtime:  5 ms, faster than 33.84% Memory Usage: 38.6 MB, less than 11.76 % 完成日期:02/26/2020 关键点:priority queue class Solution { public int largestSumAfterKNegations(int[]

二进制取反

一个数用二进制或十六进制标识时,其实使用这个数的反码表示的 对一个数取反,就是对其反码取反,得到的值为反码 在C语言中,负数是以补码方式存放的,计算方法为,负数绝对值对应数值的二进制值,按位取反后再加一.当负数按位取反时,就是其补码按位取反.比如,-10在存储为char型时,10的二进制值为0000 1010,取反后为1111 0101, 加一得到补码的二进制值为 1111 0110, 所以-10在存为char型时,补码的16进制值形式为0xF6.当-10取反时,就是把1111 0110取反,结

~取反

~用于取反,~6=-7. 0000 0000 0000 0000 0000 0000 0000 0110取反后是: 1111 1111 1111 1111 1111 1111 1111 1001 (-7) 对上面数-1再取反 减1后是 1111 1111 1111 1111 1111 1111 1111 1000 取反后是 0000 0000 0000 0000 0000 0000 0000 0111(7) 原文地址:https://www.cnblogs.com/hongxiao2020/p/

位运算常用操作总结位运算应用口诀清零取反要用与,某位置一可用或若要取反和交换,轻轻松松用异或移位运

来源:http://www.educity.cn/wenda/381487.html 位运算常用操作总结位运算应用口诀 清零取反要用与,某位置一可用或 若要取反和交换,轻轻松松用异或 移位运算 要点 1 它们都是双目运算符,两个运算分量都是整形,结果也是整形.     2 " $amp;     3 "$amp;>amp;>quot;$右移:右边的位被挤掉.对于左边移出的空位,如果是正数则空位补0,若为负数,可能补0或补1,这取决于所用的计算机系统.     4 "

[LeetCode] K sum(2Sum、3Sum、4Sum)

1 2Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please not