<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>negatives Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/negatives/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/negatives/</link>
	<description></description>
	<lastBuildDate>Fri, 31 Dec 2021 23:08:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>negatives Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/negatives/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1975. Maximum Matrix Sum</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 23:04:43 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[flip]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[negatives]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9350</guid>

					<description><![CDATA[<p>You are given an&#160;n x n&#160;integer&#160;matrix. You can do the following operation&#160;any&#160;number of times: Choose any two&#160;adjacent&#160;elements of&#160;matrix&#160;and&#160;multiply&#160;each of them by&#160;-1. Two elements are considered&#160;adjacent&#160;if&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/">花花酱 LeetCode 1975. Maximum Matrix Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<ul><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>



<p>Two elements are considered&nbsp;<strong>adjacent</strong>&nbsp;if and only if they share a&nbsp;<strong>border</strong>.</p>



<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>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[1,-1],[-1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can follow the following steps to reach sum equals 4:
- Multiply the 2 elements in the first row by -1.
- Multiply the 2 elements in the first column by -1.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
<strong>Output:</strong> 16
<strong>Explanation:</strong> We can follow the following step to reach sum equals 16:
- Multiply the 2 last elements in the second row by -1.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Math</strong></h2>



<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>



<p>Time complexity: O(n<sup>2</sup>)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  long long maxMatrixSum(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
    const int n = matrix.size();
    long long ans = 0;
    int count = 0;
    int lo = INT_MAX;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; n; ++j) {
        ans += abs(matrix[i][j]);
        lo = min(lo, abs(matrix[i][j]));
        count += matrix[i][j] &lt; 0;          
      }    
    return ans - (count &amp; 1) * 2 * lo;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/">花花酱 LeetCode 1975. Maximum Matrix Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-1975-maximum-matrix-sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
