{"id":4956,"date":"2019-03-09T21:58:33","date_gmt":"2019-03-10T05:58:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4956"},"modified":"2019-03-09T21:59:27","modified_gmt":"2019-03-10T05:59:27","slug":"leetcode-1005-maximize-sum-of-array-after-k-negations","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1005-maximize-sum-of-array-after-k-negations\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1005. Maximize Sum Of Array After K Negations"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>A<\/code>&nbsp;of integers, we&nbsp;<strong>must<\/strong>&nbsp;modify the array in the following way: we choose an&nbsp;<code>i<\/code>&nbsp;and replace&nbsp;<code>A[i]<\/code>&nbsp;with&nbsp;<code>-A[i]<\/code>, and we repeat this process&nbsp;<code>K<\/code>&nbsp;times in total.&nbsp; (We may choose the same index&nbsp;<code>i<\/code>&nbsp;multiple times.)<\/p>\n\n\n\n<p>Return the largest possible sum of the array after modifying it in this way.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [4,2,3], K = 1\n<strong>Output: <\/strong>5\n<strong>Explanation: <\/strong>Choose indices (1,) and A becomes [4,-2,3].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [3,-1,0,2], K = 3\n<strong>Output: <\/strong>6\n<strong>Explanation: <\/strong>Choose indices (1, 2, 2) and A becomes [3,1,0,2].\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [2,-3,-1,5,-4], K = 2\n<strong>Output: <\/strong>13\n<strong>Explanation: <\/strong>Choose indices (1, 4) and A becomes [2,3,-1,5,4].\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 10000<\/code><\/li><li><code>1 &lt;= K &lt;= 10000<\/code><\/li><li><code>-100 &lt;= A[i] &lt;= 100<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sorting<\/strong><\/h2>\n\n\n\n<p>Flip as many as negative numbers as possible, if K &gt; # of negative numbers and K is odd, flip the smallest element in the array.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua 8 ms 8.6MB\nclass Solution {\npublic:\n  int largestSumAfterKNegations(vector<int>& A, int K) {    \n    std::sort(begin(A), end(A));\n    for (int& a : A) {\n      if (!K) break;\n      if (a < 0) {\n        a = -a;\n        --K;\n      }\n    }\n    return accumulate(begin(A), end(A), 0) - (K % 2 ? *min_element(begin(A), end(A)) * 2 : 0);    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;A&nbsp;of integers, we&nbsp;must&nbsp;modify the array in the following way: we choose an&nbsp;i&nbsp;and replace&nbsp;A[i]&nbsp;with&nbsp;-A[i], and we repeat this process&nbsp;K&nbsp;times in total.&nbsp; (We may choose&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[222,88],"class_list":["post-4956","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-easy","tag-greedy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4956"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4956\/revisions"}],"predecessor-version":[{"id":4958,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4956\/revisions\/4958"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}