1005. Maximize Sum Of Array After K Negations Easy

@problem@discussion
#Array#Greedy#Sorting



1/**
2 * [1005] Maximize Sum Of Array After K Negations
3 *
4 * Given an integer array nums and an integer k, modify the array in the following way:
5 * 
6 * 	choose an index i and replace nums[i] with -nums[i].
7 * 
8 * You should apply this process exactly k times. You may choose the same index i multiple times.
9 * Return the largest possible sum of the array after modifying it in this way.
10 *  
11 * Example 1:
12 * 
13 * Input: nums = [4,2,3], k = 1
14 * Output: 5
15 * Explanation: Choose index 1 and nums becomes [4,-2,3].
16 * 
17 * Example 2:
18 * 
19 * Input: nums = [3,-1,0,2], k = 3
20 * Output: 6
21 * Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].
22 * 
23 * Example 3:
24 * 
25 * Input: nums = [2,-3,-1,5,-4], k = 2
26 * Output: 13
27 * Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	1 <= nums.length <= 10^4
33 * 	-100 <= nums[i] <= 100
34 * 	1 <= k <= 10^4
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/
40// discuss: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn largest_sum_after_k_negations(nums: Vec<i32>, k: i32) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_1005() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.