{"id":9350,"date":"2021-12-31T15:04:43","date_gmt":"2021-12-31T23:04:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9350"},"modified":"2021-12-31T15:08:58","modified_gmt":"2021-12-31T23:08:58","slug":"leetcode-1975-maximum-matrix-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1975-maximum-matrix-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1975. Maximum Matrix Sum"},"content":{"rendered":"\n<p>You are given an&nbsp;<code>n x n<\/code>&nbsp;integer&nbsp;<code>matrix<\/code>. You can do the following operation&nbsp;<strong>any<\/strong>&nbsp;number of times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choose any two&nbsp;<strong>adjacent<\/strong>&nbsp;elements of&nbsp;<code>matrix<\/code>&nbsp;and&nbsp;<strong>multiply<\/strong>&nbsp;each of them by&nbsp;<code>-1<\/code>.<\/li><\/ul>\n\n\n\n<p>Two elements are considered&nbsp;<strong>adjacent<\/strong>&nbsp;if and only if they share a&nbsp;<strong>border<\/strong>.<\/p>\n\n\n\n<p>Your goal is to&nbsp;<strong>maximize<\/strong>&nbsp;the summation of the matrix&#8217;s elements. Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;sum of the matrix&#8217;s elements using the operation mentioned above.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/07\/16\/pc79-q2ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[1,-1],[-1,1]]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> We can follow the following steps to reach sum equals 4:\n- Multiply the 2 elements in the first row by -1.\n- Multiply the 2 elements in the first column by -1.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/07\/16\/pc79-q2ex2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]\n<strong>Output:<\/strong> 16\n<strong>Explanation:<\/strong> We can follow the following step to reach sum equals 16:\n- Multiply the 2 last elements in the second row by -1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == matrix.length == matrix[i].length<\/code><\/li><li><code>2 &lt;= n &lt;= 250<\/code><\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= matrix[i][j] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Count the number of negative numbers. <br>1. Even negatives, we can always flip all the negatives to positives. ans = sum(abs(matrix)). <br>2. Odd negatives, there will be one negative left, we found the smallest abs(element) and let it become negative. ans = sum(abs(matrix))) &#8211; 2 * min(abs(matrix))<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<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\nclass Solution {\npublic:\n  long long maxMatrixSum(vector<vector<int>>& matrix) {\n    const int n = matrix.size();\n    long long ans = 0;\n    int count = 0;\n    int lo = INT_MAX;\n    for (int i = 0; i < n; ++i)\n      for (int j = 0; j < n; ++j) {\n        ans += abs(matrix[i][j]);\n        lo = min(lo, abs(matrix[i][j]));\n        count += matrix[i][j] < 0;          \n      }    \n    return ans - (count &#038; 1) * 2 * lo;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given an&nbsp;n x n&nbsp;integer&nbsp;matrix. You can do the following operation&nbsp;any&nbsp;number of times: Choose any two&nbsp;adjacent&nbsp;elements of&nbsp;matrix&nbsp;and&nbsp;multiply&nbsp;each of them by&nbsp;-1. Two elements are considered&nbsp;adjacent&nbsp;if&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[302,31,216,177,757],"class_list":["post-9350","post","type-post","status-publish","format-standard","hentry","category-math","tag-flip","tag-math","tag-matrix","tag-medium","tag-negatives","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9350","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=9350"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9350\/revisions"}],"predecessor-version":[{"id":9354,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9350\/revisions\/9354"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}