JS | JavaScript | Greedy | Easy To Understand | Solution
\n## Optimized Solution\nTC :\nSC: \n\n\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar largestSumAfterKNegations = functio
\n## Optimized Solution\nTC :\nSC: \n\n```\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar largestSumAfterKNegations = function(nums, k) {    \n    nums.sort((a, b) => a - b);\n    let modify = 0;\n    let i = 0;\n    while (k > 0) {\n        if (nums[i] < nums[modify]) modify = i;\n        if (i + 1 < nums.length) i ++;\n        nums[modify] = -nums[modify];\n        k --;\n    }\n    return nums.reduce((acc, cur) => acc + cur, 0);\n};\n```\n\n\n## Easy To Understand \n- slow\n```\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar largestSumAfterKNegations = function(nums, k) {\n    let i = 0;\n    while (i < k) {\n        nums.sort((a, b) => a - b);\n        nums[0] = -nums[0];\n        i ++;\n    }\n    return nums.reduce((acc, cur) => acc + cur, 0);\n};\n```
['Greedy', 'JavaScript']