<?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>max element Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/max-element/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/max-element/</link>
	<description></description>
	<lastBuildDate>Sun, 31 May 2020 16:34:15 +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>max element Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/max-element/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1464. Maximum Product of Two Elements in an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1464-maximum-product-of-two-elements-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1464-maximum-product-of-two-elements-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 May 2020 16:18:17 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[max element]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6854</guid>

					<description><![CDATA[<p>Given the array of integers&#160;nums, you will choose two different indices&#160;i&#160;and&#160;j&#160;of that array.&#160;Return the maximum value of(nums[i]-1)*(nums[j]-1). Example 1: Input: nums = [3,4,5,2] Output: 12&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1464-maximum-product-of-two-elements-in-an-array/">花花酱 LeetCode 1464. Maximum Product of Two Elements in an Array</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>Given the array of integers&nbsp;<code>nums</code>, you will choose two different indices&nbsp;<code>i</code>&nbsp;and&nbsp;<code>j</code>&nbsp;of that array.&nbsp;<em>Return the maximum value of</em><code>(nums[i]-1)*(nums[j]-1)</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,4,5,2]
<strong>Output:</strong> 12 
<strong>Explanation:</strong> If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,5,4,5]
<strong>Output:</strong> 16
<strong>Explanation:</strong> Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,7]
<strong>Output:</strong> 12
</pre>



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



<ul><li><code>2 &lt;= nums.length &lt;= 500</code></li><li><code>1 &lt;= nums[i] &lt;= 10^3</code></li></ul>



<h2><strong>Solution 1: Sort</strong></h2>



<p>We want to find the largest and second largest elements.</p>



<p>Time complexity: O(nlogn)<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:
  int maxProduct(vector&lt;int&gt;&amp; nums) {
    sort(rbegin(nums), rend(nums));
    return (nums[0] - 1) * (nums[1] - 1);
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Without sorting</strong></h2>



<p>Time complexity: O(n)<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:
  int maxProduct(vector&lt;int&gt;&amp; nums) {
    int m1 = 0;
    int m2 = 0;
    for (int n : nums) {
      if (n &gt; m1) {
        m2 = m1;
        m1 = n;
      } else if (n &gt; m2) {
        m2 = n;
      }
    }
    return (m1 - 1) * (m2 - 1);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1464-maximum-product-of-two-elements-in-an-array/">花花酱 LeetCode 1464. Maximum Product of Two Elements in an Array</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/algorithms/array/leetcode-1464-maximum-product-of-two-elements-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 515. Find Largest Value in Each Tree Row</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Mar 2018 05:08:33 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[inorder]]></category>
		<category><![CDATA[max element]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[travesal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1982</guid>

					<description><![CDATA[<p>题目大意：给你一棵树，返回每一层最大的元素的值。 You need to find the largest value in each row of a binary tree. Example: [crayon-66402ee39992a097909044/] Solution 1: Inorder traversal + depth info C++&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/">花花酱 LeetCode 515. Find Largest Value in Each Tree Row</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description">
<p>题目大意：给你一棵树，返回每一层最大的元素的值。</p>
<p>You need to find the largest value in each row of a binary tree.</p>
<p><b>Example:</b></p><pre class="crayon-plain-tag">&lt;b&gt;Input:&lt;/b&gt; 

          1
         / \
        3   2
       / \   \  
      5   3   9 

&lt;b&gt;Output:&lt;/b&gt; [1, 3, 9]</pre><p>
</div>
<p>Solution 1: Inorder traversal + depth info</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 15 ms
class Solution {
public:
  vector&lt;int&gt; largestValues(TreeNode* root) {
    vector&lt;int&gt; ans;
    inorder(root, 0, ans);
    return ans;
  }
private:
  void inorder(TreeNode* root, int d, vector&lt;int&gt;&amp; ans) {
    if (root == nullptr) return;
    while (ans.size() &lt;= d) ans.push_back(INT_MIN);    
    inorder(root-&gt;left, d + 1, ans);
    ans[d] = max(ans[d], root-&gt;val);
    inorder(root-&gt;right, d + 1, ans);
  }
};</pre><p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 72 ms (beats 100%)
"""
class Solution:
  def largestValues(self, root):
    def inorder(root, d, ans):
      if not root: return ans
      if len(ans) &lt;= d: ans += [float('-inf')]
      inorder(root.left, d + 1, ans)
      if root.val &gt; ans[d]: ans[d] = root.val
      inorder(root.right, d + 1, ans)
      return ans
    
    return inorder(root, 0, [])</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-515-find-largest-value-in-each-tree-row/">花花酱 LeetCode 515. Find Largest Value in Each Tree Row</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/tree/leetcode-515-find-largest-value-in-each-tree-row/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 795. Number of Subarrays with Bounded Maximum</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-795-number-of-subarrays-with-bounded-maximum/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-795-number-of-subarrays-with-bounded-maximum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 04 Mar 2018 05:36:43 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[max element]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[subarray]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1933</guid>

					<description><![CDATA[<p>题目大意：问一个数组中有多少个子数组的最大元素值在[L, R]的范围里。 We are given an array A of positive integers, and two positive integers L and R (L &#60;= R). Return the number of (contiguous, non-empty) subarrays such that the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-795-number-of-subarrays-with-bounded-maximum/">花花酱 LeetCode 795. Number of Subarrays with Bounded Maximum</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>题目大意：问一个数组中有多少个子数组的最大元素值在[L, R]的范围里。</p>
<p>We are given an array <code>A</code> of positive integers, and two positive integers <code>L</code> and <code>R</code> (<code>L &lt;= R</code>).</p>
<p>Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least <code>L</code> and at most <code>R</code>.</p><pre class="crayon-plain-tag">Example :
Input: 
A = [2, 1, 4, 3]
L = 2
R = 3
Output: 3
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].</pre><p>Solution 1:</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 42 ms
class Solution {
public:
  int numSubarrayBoundedMax(vector&lt;int&gt;&amp; A, int L, int R) {
    return count(A, R) - count(A, L - 1);
  }
private:
  // Count # of sub arrays whose max element is &lt;= N
  int count(const vector&lt;int&gt;&amp; A, int N) {
    int ans = 0;
    int cur = 0;
    for (int a: A) {
      if (a &lt;= N) 
        ans += ++cur;
      else
        cur = 0;
    }
    return ans;
  }
};</pre><p>Solution 2: One pass</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 38 ms
class Solution {
public:
  int numSubarrayBoundedMax(vector&lt;int&gt;&amp; A, int L, int R) {
    int ans = 0;
    int left = -1;
    int right = -1;
    for (int i = 0; i &lt; A.size(); ++i) {
      if (A[i] &gt;= L) right = i;
      if (A[i] &gt; R) left = i;      
      ans += (right - left);
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-795-number-of-subarrays-with-bounded-maximum/">花花酱 LeetCode 795. Number of Subarrays with Bounded Maximum</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/algorithms/array/leetcode-795-number-of-subarrays-with-bounded-maximum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
