bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1005. Maximize Sum Of Array After K Negations

1005. Maximize Sum Of Array After K Negations

Leetcode Solutions

Greedy Approach with Sorting

  1. Sort the array nums in non-decreasing order.
  2. Iterate over the sorted array and for each negative number, if k is greater than 0, negate the number and decrement k.
  3. If k is still greater than 0 after negating all negative numbers, check if k is odd.
  4. If k is odd, negate the smallest absolute value in the array (which will be the first element after sorting).
  5. Calculate the sum of the array and return it.
UML Thumbnail

Min Heap Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...