<?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>easy &#8211; Huahua&#8217;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/easy/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 02 Mar 2026 11:20:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.4</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>easy &#8211; Huahua&#8217;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>LeetCode 3512. Minimum Operations to Make Array Sum Divisible by K</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-3512-minimum-operations-to-make-array-sum-divisible-by-k/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-3512-minimum-operations-to-make-array-sum-divisible-by-k/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 16 Apr 2025 01:17:03 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10351</guid>

					<description><![CDATA[求合取余即可，余数就是答案。 Time complexity: O(n)Space complexity: O(1) [crayon-69a6f65138887550176593/]]]></description>
										<content:encoded><![CDATA[
<p>求合取余即可，余数就是答案。</p>



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



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int minOperations(vector&lt;int&gt;&amp; nums, int k) {
    return accumulate(begin(nums), end(nums), 0) % k;
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-3512-minimum-operations-to-make-array-sum-divisible-by-k/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2011. Final Value of Variable After Performing Operations</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-2011-final-value-of-variable-after-performing-operations/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-2011-final-value-of-variable-after-performing-operations/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 11 Apr 2025 04:04:13 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10322</guid>

					<description><![CDATA[C++ 也能写one-liner了。 时间复杂度：O(n)空间复杂度：O(1) [crayon-69a6f651393b5068817669/]]]></description>
										<content:encoded><![CDATA[
<p>C++ 也能写one-liner了。</p>



<p>时间复杂度：O(n)<br>空间复杂度：O(1)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int finalValueAfterOperations(vector&lt;string&gt;&amp; operations) {
    return accumulate(begin(operations), end(operations), 0, [](const auto s, const auto&amp; op){
      return s + (op[1] == '+' ? 1 : -1);
    });
  }
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-2011-final-value-of-variable-after-performing-operations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 3242. Design Neighbor Sum Service</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-3242-design-neighbor-sum-service/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-3242-design-neighbor-sum-service/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Mar 2025 22:36:51 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[precompute]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10238</guid>

					<description><![CDATA[青铜 Brute Force：每次需要花费O(n*n)的时间去查找query所在的格子，求和是O(1)。总的时间复杂度达到O(n^4)，肯定会超时。 白银 Hashtable：由于所有元素的值的唯一的，我们可以使用hashtable（或者数组）来记录value所在的格子坐标，这样每次Query都是O(1)时间。时间复杂度：初始化O(n^2)，query O(1)。空间复杂度：O(n^2) [crayon-69a6f6513973d967662604/] 黄金 Precompute：在初始化的时候顺便求合，开两个数组一个存上下左右的，一个存对角线的。query的时候直接返回答案就可以了。时间复杂度和白银是一样的，但会快一些（省去了求合的过程）。 [crayon-69a6f65139747114707543/]]]></description>
										<content:encoded><![CDATA[
<p>青铜 Brute Force：每次需要花费O(n*n)的时间去查找query所在的格子，求和是O(1)。总的时间复杂度达到O(n^4)，肯定会超时。</p>



<p>白银 Hashtable：由于所有元素的值的唯一的，我们可以使用hashtable（或者数组）来记录value所在的格子坐标，这样每次Query都是O(1)时间。<br>时间复杂度：初始化O(n^2)，query O(1)。<br>空间复杂度：O(n^2)</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class NeighborSum {
public:
  NeighborSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid): 
    n_(grid.size()), g_(&amp;grid), xy_(n_ * n_) {
    for (int i = 0; i &lt; n_; ++i)
      for (int j = 0; j &lt; n_; ++j)
        xy_[grid[i][j]] = {j, i};
  }
  
  int adjacentSum(int value) {
    auto [x, y] = xy_[value];
    return val(x, y - 1) + val(x, y + 1) + val(x + 1, y) + val(x - 1, y);
  }
  
  int diagonalSum(int value) {
    auto [x, y] = xy_[value];
    return val(x - 1, y - 1) + val(x - 1, y + 1) + val(x + 1, y - 1) + val(x + 1, y + 1);
  }
private:
  inline int val(int x, int y) const {
    if (x &lt; 0 || x &gt;= n_ || y &lt; 0 || y &gt;= n_) return 0;
    return (*g_)[y][x];
  }
  int n_;
  vector&lt;vector&lt;int&gt;&gt;* g_;
  vector&lt;pair&lt;int, int&gt;&gt; xy_;
};</pre>



<p>黄金 Precompute：在初始化的时候顺便求合，开两个数组一个存上下左右的，一个存对角线的。query的时候直接返回答案就可以了。<br>时间复杂度和白银是一样的，但会快一些（省去了求合的过程）。</p>



<pre class="urvanov-syntax-highlighter-plain-tag">class NeighborSum {
public:
  NeighborSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    int n = grid.size();
    adj_.resize(n * n);
    dia_.resize(n * n);
    auto v = [&amp;](int x, int y) -&gt; int {
      if (x &lt; 0 || x &gt;= n || y &lt; 0 || y &gt;= n) 
        return 0;
      return grid[y][x];
    };
    for (int y = 0; y &lt; n; ++y)
      for (int x = 0; x &lt; n; ++x) {
        adj_[grid[y][x]] = v(x, y - 1) + v(x, y + 1) + v(x + 1, y) + v(x - 1, y);
        dia_[grid[y][x]] = v(x - 1, y - 1) + v(x - 1, y + 1) + v(x + 1, y - 1) + v(x + 1, y + 1);
      }
  }
  
  int adjacentSum(int value) {
    return adj_[value];
  }
  
  int diagonalSum(int value) {
    return dia_[value];
  }
private:
  vector&lt;int&gt; adj_;
  vector&lt;int&gt; dia_;
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-3242-design-neighbor-sum-service/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 3028. Ant on the Boundary</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-3028-ant-on-the-boundary/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-3028-ant-on-the-boundary/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Feb 2024 01:49:25 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10110</guid>

					<description><![CDATA[An ant is on a boundary. It sometimes goes&#160;left&#160;and sometimes&#160;right. You are given an array of&#160;non-zero&#160;integers&#160;nums. The ant starts reading&#160;nums&#160;from the first element of it&#8230;]]></description>
										<content:encoded><![CDATA[
<p>An ant is on a boundary. It sometimes goes&nbsp;<strong>left</strong>&nbsp;and sometimes&nbsp;<strong>right</strong>.</p>



<p>You are given an array of&nbsp;<strong>non-zero</strong>&nbsp;integers&nbsp;<code>nums</code>. The ant starts reading&nbsp;<code>nums</code>&nbsp;from the first element of it to its end. At each step, it moves according to the value of the current element:</p>



<ul class="wp-block-list"><li>If&nbsp;<code>nums[i] &lt; 0</code>, it moves&nbsp;<strong>left</strong>&nbsp;by&nbsp;<code>-nums[i]</code>&nbsp;units.</li><li>If&nbsp;<code>nums[i] &gt; 0</code>, it moves&nbsp;<strong>right</strong>&nbsp;by&nbsp;<code>nums[i]</code>&nbsp;units.</li></ul>



<p>Return&nbsp;<em>the number of times the ant&nbsp;<strong>returns</strong>&nbsp;to the boundary.</em></p>



<p><strong>Notes:</strong></p>



<ul class="wp-block-list"><li>There is an infinite space on both sides of the boundary.</li><li>We check whether the ant is on the boundary only after it has moved&nbsp;<code>|nums[i]|</code>&nbsp;units. In other words, if the ant crosses the boundary during its movement, it does not count.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,3,-5]
<strong>Output:</strong> 1
<strong>Explanation:</strong> After the first step, the ant is 2 steps to the right of the boundary.
After the second step, the ant is 5 steps to the right of the boundary.
After the third step, the ant is on the boundary.
So the answer is 1.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,2,-3,-4]
<strong>Output:</strong> 0
<strong>Explanation:</strong> After the first step, the ant is 3 steps to the right of the boundary.
After the second step, the ant is 5 steps to the right of the boundary.
After the third step, the ant is 2 steps to the right of the boundary.
After the fourth step, the ant is 2 steps to the left of the boundary.
The ant never returned to the boundary, so the answer is 0.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>-10 &lt;= nums[i] &lt;= 10</code></li><li><code>nums[i] != 0</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Simulation </strong></h2>



<p>Simulate the position of the ant by summing up the numbers. If the position is zero (at boundary), increase the answer by 1.</p>



<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="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int returnToBoundaryCount(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    int pos = 0;
    for (int x : nums)
      ans += !(pos += x);
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-3028-ant-on-the-boundary/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 3005. Count Elements With Maximum Frequency</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-3005-count-elements-with-maximum-frequency/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-3005-count-elements-with-maximum-frequency/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 16 Jan 2024 01:56:31 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[frequency]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10104</guid>

					<description><![CDATA[You are given an array&#160;nums&#160;consisting of&#160;positive&#160;integers. Return&#160;the&#160;total frequencies&#160;of elements in&#160;nums&#160;such that those elements all have the&#160;maximum&#160;frequency. The&#160;frequency&#160;of an element is the number of occurrences of&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given an array&nbsp;<code>nums</code>&nbsp;consisting of&nbsp;<strong>positive</strong>&nbsp;integers.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>total frequencies</strong>&nbsp;of elements in</em><em>&nbsp;</em><code>nums</code>&nbsp;<em>such that those elements all have the&nbsp;<strong>maximum</strong>&nbsp;frequency</em>.</p>



<p>The&nbsp;<strong>frequency</strong>&nbsp;of an element is the number of occurrences of that element in the array.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,2,3,1,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 5
<strong>Explanation:</strong> All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 100</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Hashtable</strong></h2>



<p>Use a hashtable to store the frequency of each element, and compare it with a running maximum frequency. Reset answer if current frequency is greater than maximum frequency. Increment the answer if current frequency is equal to the maximum frequency.</p>



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



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

// Author: Huahua
<pre class="urvanov-syntax-highlighter-plain-tag">class Solution {
public:
  int maxFrequencyElements(vector&lt;int&gt;&amp; nums) {
    unordered_map&lt;int, int&gt; m;
    int freq = 0;
    int ans = 0;
    for (int x : nums) {
      if (++m[x] &gt; freq)
        freq = m[x], ans = 0;
      if (m[x] == freq)
        ans += m[x];
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-3005-count-elements-with-maximum-frequency/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2828. Check if a String Is an Acronym of Words</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2828-check-if-a-string-is-an-acronym-of-words/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2828-check-if-a-string-is-an-acronym-of-words/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 26 Aug 2023 16:29:43 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10082</guid>

					<description><![CDATA[Given an array of strings&#160;words&#160;and a string&#160;s, determine if&#160;s&#160;is an&#160;acronym&#160;of words. The string&#160;s&#160;is considered an acronym of&#160;words&#160;if it can be formed by concatenating the&#160;first&#160;character of&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given an array of strings&nbsp;<code>words</code>&nbsp;and a string&nbsp;<code>s</code>, determine if&nbsp;<code>s</code>&nbsp;is an&nbsp;<strong>acronym</strong>&nbsp;of words.</p>



<p>The string&nbsp;<code>s</code>&nbsp;is considered an acronym of&nbsp;<code>words</code>&nbsp;if it can be formed by concatenating the&nbsp;<strong>first</strong>&nbsp;character of each string in&nbsp;<code>words</code>&nbsp;<strong>in order</strong>. For example,&nbsp;<code>"ab"</code>&nbsp;can be formed from&nbsp;<code>["apple", "banana"]</code>, but it can&#8217;t be formed from&nbsp;<code>["bear", "aardvark"]</code>.</p>



<p>Return&nbsp;<code>true</code><em>&nbsp;if&nbsp;</em><code>s</code><em>&nbsp;is an acronym of&nbsp;</em><code>words</code><em>, and&nbsp;</em><code>false</code><em>&nbsp;otherwise.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["alice","bob","charlie"], s = "abc"
<strong>Output:</strong> true
<strong>Explanation:</strong> The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym. 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["an","apple"], s = "a"
<strong>Output:</strong> false
<strong>Explanation:</strong> The first character in the words "an" and "apple" are 'a' and 'a', respectively. 
The acronym formed by concatenating these characters is "aa". 
Hence, s = "a" is not the acronym.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["never","gonna","give","up","on","you"], s = "ngguoy"
<strong>Output:</strong> true
<strong>Explanation: </strong>By concatenating the first character of the words in the array, we get the string "ngguoy". 
Hence, s = "ngguoy" is the acronym.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= words.length &lt;= 100</code></li><li><code>1 &lt;= words[i].length &lt;= 10</code></li><li><code>1 &lt;= s.length &lt;= 100</code></li><li><code>words[i]</code>&nbsp;and&nbsp;<code>s</code>&nbsp;consist of lowercase English letters.<br></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Check the first letter of each word</strong></h2>



<p>No need to concatenate, just check the first letter of each word.</p>



<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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  bool isAcronym(vector&lt;string&gt;&amp; words, string_view s) {
    if (words.size() != s.size()) return false;
    for (int i = 0; i &lt; s.size(); ++i)
      if (words[i][0] != s[i]) return false;
    return true;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-2828-check-if-a-string-is-an-acronym-of-words/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2824. Count Pairs Whose Sum is Less than Target</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2824-count-pairs-whose-sum-is-less-than-target/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2824-count-pairs-whose-sum-is-less-than-target/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 24 Aug 2023 01:09:34 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10070</guid>

					<description><![CDATA[Given a&#160;0-indexed&#160;integer array&#160;nums&#160;of length&#160;n&#160;and an integer&#160;target, return&#160;the number of pairs(i, j)where0 &#60;= i &#60; j &#60; nandnums[i] + nums[j] &#60; target. Example 1: Input: nums&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;and an integer&nbsp;<code>target</code>, return&nbsp;<em>the number of pairs</em><code>(i, j)</code><em>where</em><code>0 &lt;= i &lt; j &lt; n</code><em>and</em><code>nums[i] + nums[j] &lt; target</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-1,1,2,3,1], target = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = 0 &lt; target
- (0, 2) since 0 &lt; 2 and nums[0] + nums[2] = 1 &lt; target 
- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = 0 &lt; target
Note that (0, 3) is not counted since nums[0] + nums[3] is not strictly less than the target.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [-6,2,5,-2,-7,-1,3], target = -2
<strong>Output:</strong> 10
<strong>Explanation:</strong> There are 10 pairs of indices that satisfy the conditions in the statement:
- (0, 1) since 0 &lt; 1 and nums[0] + nums[1] = -4 &lt; target
- (0, 3) since 0 &lt; 3 and nums[0] + nums[3] = -8 &lt; target
- (0, 4) since 0 &lt; 4 and nums[0] + nums[4] = -13 &lt; target
- (0, 5) since 0 &lt; 5 and nums[0] + nums[5] = -7 &lt; target
- (0, 6) since 0 &lt; 6 and nums[0] + nums[6] = -3 &lt; target
- (1, 4) since 1 &lt; 4 and nums[1] + nums[4] = -5 &lt; target
- (3, 4) since 3 &lt; 4 and nums[3] + nums[4] = -9 &lt; target
- (3, 5) since 3 &lt; 5 and nums[3] + nums[5] = -3 &lt; target
- (4, 5) since 4 &lt; 5 and nums[4] + nums[5] = -8 &lt; target
- (4, 6) since 4 &lt; 6 and nums[4] + nums[6] = -4 &lt; target
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length == n &lt;= 50</code></li><li><code>-50 &lt;= nums[i], target &lt;= 50</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution 1: Brute force</strong></h2>



<p>Enumerate all pairs.</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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int countPairs(vector&lt;int&gt;&amp; nums, int target) {
    const int n = nums.size();
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        ans += nums[i] + nums[j] &lt; target;
    return ans;
  }
};</pre>
</div></div>



<h2 class="wp-block-heading"><strong>Solution 2: Two Pointers</strong></h2>



<p>Sort the numbers.</p>



<p>Use two pointers i, and j.<br>Set i to 0 and j to n &#8211; 1.<br>while (nums[i] + nums[j] >= target) &#8211;j<br>then we have nums[i] + nums[k] &lt; target (i &lt; k &lt;= j), in total (j &#8211; i) pairs.<br>++i, move to the next starting number.<br>Time complexity: O(nlogn + n)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int countPairs(vector&lt;int&gt;&amp; nums, int target) {
    const int n = nums.size();
    sort(begin(nums), end(nums));
    int ans = 0;
    for (int i = 0, j = n - 1; i &lt; n; ++i) {
      while (j &gt;= i &amp;&amp; nums[i] + nums[j] &gt;= target) --j;      
      ans += max(0, j - i);
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2824-count-pairs-whose-sum-is-less-than-target/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2815. Max Pair Sum in an Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 13 Aug 2023 21:40:33 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[digit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[pair]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10067</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;integer array&#160;nums. You have to find the&#160;maximum&#160;sum of a pair of numbers from&#160;nums&#160;such that the maximum&#160;digit&#160;in both numbers are equal. Return&#160;the maximum&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>. You have to find the&nbsp;<strong>maximum</strong>&nbsp;sum of a pair of numbers from&nbsp;<code>nums</code>&nbsp;such that the maximum&nbsp;<strong>digit&nbsp;</strong>in both numbers are equal.</p>



<p>Return&nbsp;<em>the maximum sum or</em>&nbsp;<code>-1</code><em>&nbsp;if no such pair exists</em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [51,71,17,24,42]
<strong>Output:</strong> 88
<strong>Explanation:</strong> 
For i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. 
For i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66.
It can be shown that there are no other pairs with equal maximum digits, so the answer is 88.</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> -1
<strong>Explanation:</strong> No pair exists in nums with equal maximum digits.
</pre>



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



<ul class="wp-block-list"><li><code>2 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Brute Force</strong></h2>



<p>Enumerate all pairs of nums and check their sum and max digit.</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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int maxSum(vector&lt;int&gt;&amp; nums) {
    auto maxDigit = [](int x) {
      int ans = 0;
      while (x) {
        ans = max(ans, x % 10);
        x /= 10;
      }
      return ans;
    };
    int ans = -1;
    const int n = nums.size();
    for (int i = 0; i &lt; n; ++i)
      for (int j = i + 1; j &lt; n; ++j)
        if (nums[i] + nums[j] &gt; ans 
            &amp;&amp; maxDigit(nums[i]) == maxDigit(nums[j]))
          ans = nums[i] + nums[j];
    return ans;
  }
};</pre>
</div></div>

]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2815-max-pair-sum-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2769. Find the Maximum Achievable Number</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-2769-find-the-maximum-achievable-number/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-2769-find-the-maximum-achievable-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 12 Jul 2023 01:12:33 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[greedy]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10057</guid>

					<description><![CDATA[You are given two integers,&#160;num&#160;and&#160;t. An integer&#160;x&#160;is called&#160;achievable&#160;if it can become equal to&#160;num&#160;after applying the following operation no more than&#160;t&#160;times: Increase or decrease&#160;x&#160;by&#160;1, and simultaneously&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given two integers,&nbsp;<code>num</code>&nbsp;and&nbsp;<code>t</code>.</p>



<p>An integer&nbsp;<code>x</code>&nbsp;is called&nbsp;<strong>achievable</strong>&nbsp;if it can become equal to&nbsp;<code>num</code>&nbsp;after applying the following operation no more than&nbsp;<code>t</code>&nbsp;times:</p>



<ul class="wp-block-list"><li>Increase or decrease&nbsp;<code>x</code>&nbsp;by&nbsp;<code>1</code>, and simultaneously increase or decrease&nbsp;<code>num</code>&nbsp;by&nbsp;<code>1</code>.</li></ul>



<p>Return&nbsp;<em>the maximum possible achievable number</em>. It can be proven that there exists at least one achievable number.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 4, t = 1
<strong>Output:</strong> 6
<strong>Explanation:</strong> The maximum achievable number is x = 6; it can become equal to num after performing this operation:
1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. 
It can be proven that there is no achievable number larger than 6.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> num = 3, t = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> The maximum achievable number is x = 7; after performing these operations, x will equal num: 
1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4.
2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
It can be proven that there is no achievable number larger than 7.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= num, t&nbsp;&lt;= 50</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Greedy</strong></h2>



<p>Always decrease x and always increase num, the max achievable number  x = num + t * 2</p>



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



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int theMaximumAchievableX(int num, int t) {
    return num + t * 2;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-2769-find-the-maximum-achievable-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2667. Create Hello World Function</title>
		<link>https://zxi.mytechroad.com/blog/functional/leetcode-2667-create-hello-world-function/</link>
					<comments>https://zxi.mytechroad.com/blog/functional/leetcode-2667-create-hello-world-function/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 07 May 2023 17:47:56 +0000</pubDate>
				<category><![CDATA[Functional]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10049</guid>

					<description><![CDATA[Write a function&#160;createHelloWorld.&#160;It should return a new function that always returns&#160;"Hello World". Example 1: Input: args = [] Output: "Hello World" Explanation: const f =&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Write a function&nbsp;<code>createHelloWorld</code>.&nbsp;It should return a new function that always returns&nbsp;<code>"Hello World"</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> args = []
<strong>Output:</strong> "Hello World"
<strong>Explanation:</strong>
const f = createHelloWorld();
f(); // "Hello World"
The function returned by createHelloWorld should always return "Hello World".
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> args = [{},null,42]
<strong>Output:</strong> "Hello World"
<strong>Explanation:</strong>
const f = createHelloWorld();
f({}, null, 42); // "Hello World"
Any arguments could be passed to the function but it should still always return "Hello World".
</pre>



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



<ul class="wp-block-list"><li><code>0 &lt;= args.length &lt;= 10</code></li></ul>



<p>Solution: </p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
/**
 * @return {Function}
 */
var createHelloWorld = function() {
    return function(...args) {
      return &quot;Hello World&quot;;
    }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/functional/leetcode-2667-create-hello-world-function/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2660. Determine the Winner of a Bowling Game</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-2660-determine-the-winner-of-a-bowling-game/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-2660-determine-the-winner-of-a-bowling-game/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Apr 2023 16:41:18 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10036</guid>

					<description><![CDATA[You are given two&#160;0-indexed&#160;integer arrays&#160;player1&#160;and&#160;player2, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively. The bowling&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given two&nbsp;<strong>0-indexed</strong>&nbsp;integer arrays&nbsp;<code>player1</code>&nbsp;and&nbsp;<code>player2</code>, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively.</p>



<p>The bowling game consists of&nbsp;<code>n</code>&nbsp;turns, and the number of pins in each turn is exactly&nbsp;<code>10</code>.</p>



<p>Assume a player hit&nbsp;<code>x<sub>i</sub></code>&nbsp;pins in the&nbsp;<code>i<sup>th</sup></code>&nbsp;turn. The value of the&nbsp;<code>i<sup>th</sup></code>&nbsp;turn for the player is:</p>



<ul class="wp-block-list"><li><code>2x<sub>i</sub></code>&nbsp;if the player hit&nbsp;<code>10</code>&nbsp;pins in any of the previous two turns.</li><li>Otherwise, It is&nbsp;<code>x<sub>i</sub></code>.</li></ul>



<p>The score of the player is the sum of the values of their&nbsp;<code>n</code>&nbsp;turns.</p>



<p>Return</p>



<ul class="wp-block-list"><li><code>1</code>&nbsp;<em>if the score of player 1 is more than the score of player 2,</em></li><li><code>2</code>&nbsp;<em>if the score of player 2 is more than the score of player 1, and</em></li><li><code>0</code>&nbsp;<em>in case of a draw.</em></li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> player1 = [4,10,7,9], player2 = [6,5,2,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.
The score of player2 is 6 + 5 + 2 + 3 = 16.
Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> player1 = [3,5,7,6], player2 = [8,10,10,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The score of player1 is 3 + 5 + 7 + 6 = 21.
The score of player2 is 8 + 10 + 2*10 + 2*2 = 42.
Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> player1 = [2,3], player2 = [4,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The score of player1 is 2 + 3 = 5
The score of player2 is 4 + 1 = 5
The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.
</pre>



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



<ul class="wp-block-list"><li><code>n == player1.length == player2.length</code></li><li><code>1 &lt;= n &lt;= 1000</code></li><li><code>0 &lt;= player1[i], player2[i] &lt;= 10</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Simulation</strong></h2>



<p>We can write a function to compute the score of a player.</p>



<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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int isWinner(vector&lt;int&gt;&amp; player1, vector&lt;int&gt;&amp; player2) {
    auto score = [] (const vector&lt;int&gt;&amp; player) {
      int sum = 0;
      int twice = 0;
      for (int x : player) {        
        sum += twice-- &gt; 0 ? x * 2 : x;
        if (x == 10) twice = 2;
      }
      return sum;
    };
    int s1 = score(player1);
    int s2 = score(player2);
    if (s1 == s2) return 0;
    return s1 &gt; s2 ? 1 : 2;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-2660-determine-the-winner-of-a-bowling-game/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2656. Maximum Sum With Exactly K Elements</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-2656-maximum-sum-with-exactly-k-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-2656-maximum-sum-with-exactly-k-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Apr 2023 14:58:44 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10022</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;integer array&#160;nums&#160;and an integer&#160;k. Your task is to perform the following operation&#160;exactly&#160;k&#160;times in order to maximize your score: Select an element&#160;m&#160;from&#160;nums. Remove&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;integer array&nbsp;<code>nums</code>&nbsp;and an integer&nbsp;<code>k</code>. Your task is to perform the following operation&nbsp;<strong>exactly</strong>&nbsp;<code>k</code>&nbsp;times in order to maximize your score:</p>



<ol class="wp-block-list"><li>Select an element&nbsp;<code>m</code>&nbsp;from&nbsp;<code>nums</code>.</li><li>Remove the selected element&nbsp;<code>m</code>&nbsp;from the array.</li><li>Add a new element with a value of&nbsp;<code>m + 1</code>&nbsp;to the array.</li><li>Increase your score by&nbsp;<code>m</code>.</li></ol>



<p>Return&nbsp;<em>the maximum score you can achieve after performing the operation exactly</em>&nbsp;<code>k</code>&nbsp;<em>times.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,5], k = 3
<strong>Output:</strong> 18
<strong>Explanation:</strong> We need to choose exactly 3 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
So, we will return 18.
It can be proven, that 18 is the maximum answer that we can achieve.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [5,5,5], k = 2
<strong>Output:</strong> 11
<strong>Explanation:</strong> We need to choose exactly 2 elements from nums to maximize the sum.
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
So, we will return 11.
It can be proven, that 11 is the maximum answer that we can achieve.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= nums.length &lt;= 100</code></li><li><code>1 &lt;= nums[i] &lt;= 100</code></li><li><code>1 &lt;= k &lt;= 100</code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Greedy</strong></h2>



<p>Always to chose the largest element from the array.</p>



<p>We can find the largest element of the array m, then the total score will be<br>m + (m + 1) + (m + 2) + &#8230; + (m + k &#8211; 1),<br>We can use summation formula of arithmetic sequence to compute that in O(1)<br>ans = (m + (m + k &#8211; 1)) * k / 2</p>



<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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int maximizeSum(vector&lt;int&gt;&amp; nums, int k) {
    int m = *max_element(begin(nums), end(nums));
    return (m + m + k - 1) * k / 2;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/greedy/leetcode-2656-maximum-sum-with-exactly-k-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2639. Find the Width of Columns of a Grid</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-2639-find-the-width-of-columns-of-a-grid/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-2639-find-the-width-of-columns-of-a-grid/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 15:22:36 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[length]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[width]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10006</guid>

					<description><![CDATA[You are given a&#160;0-indexed&#160;m x n&#160;integer matrix&#160;grid. The width of a column is the maximum&#160;length&#160;of its integers. For example, if&#160;grid = [[-10], [3], [12]], the&#8230;]]></description>
										<content:encoded><![CDATA[
<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;<code>m x n</code>&nbsp;integer matrix&nbsp;<code>grid</code>. The width of a column is the maximum&nbsp;<strong>length&nbsp;</strong>of its integers.</p>



<ul class="wp-block-list"><li>For example, if&nbsp;<code>grid = [[-10], [3], [12]]</code>, the width of the only column is&nbsp;<code>3</code>&nbsp;since&nbsp;<code>-10</code>&nbsp;is of length&nbsp;<code>3</code>.</li></ul>



<p>Return&nbsp;<em>an integer array</em>&nbsp;<code>ans</code>&nbsp;<em>of size</em>&nbsp;<code>n</code>&nbsp;<em>where</em>&nbsp;<code>ans[i]</code>&nbsp;<em>is the width of the</em>&nbsp;<code>i<sup>th</sup></code>&nbsp;<em>column</em>.</p>



<p>The&nbsp;<strong>length</strong>&nbsp;of an integer&nbsp;<code>x</code>&nbsp;with&nbsp;<code>len</code>&nbsp;digits is equal to&nbsp;<code>len</code>&nbsp;if&nbsp;<code>x</code>&nbsp;is non-negative, and&nbsp;<code>len + 1</code>&nbsp;otherwise.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[1],[22],[333]]
<strong>Output:</strong> [3]
<strong>Explanation:</strong> In the 0<sup>th</sup> column, 333 is of length 3.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[-15,1,3],[15,7,12],[5,6,-2]]
<strong>Output:</strong> [3,1,2]
<strong>Explanation:</strong> 
In the 0<sup>th</sup> column, only -15 is of length 3.
In the 1<sup>st</sup> column, all integers are of length 1. 
In the 2<sup>nd</sup> column, both 12 and -2 are of length 2.
</pre>



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



<ul class="wp-block-list"><li><code>m == grid.length</code></li><li><code>n == grid[i].length</code></li><li><code>1 &lt;= m, n &lt;= 100</code></li><li><code>-10<sup>9</sup> &lt;= grid[r][c] &lt;= 10<sup>9</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Simulation</strong></h2>



<p>Note: width of &#8216;0&#8217; is 1.</p>



<p>Time complexity: O(m*n*log(x))<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; findColumnWidth(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {
    vector&lt;int&gt; ans;
    for (int j = 0; j &lt; grid[0].size(); ++j) {
      int maxWidth = 1;
      for (int i = 0; i &lt; grid.size(); ++i) {
        int x = grid[i][j];
        int width = 0;        
        if (x &lt; 0) {
          x = -x;
          ++width;
        }
        while (x) {
          x /= 10;
          ++width;
        }        
        maxWidth = max(maxWidth, width);
      }
      ans.push_back(maxWidth);
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/simulation/leetcode-2639-find-the-width-of-columns-of-a-grid/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2643. Row With Maximum Ones</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2643-row-with-maximum-ones/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2643-row-with-maximum-ones/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 29 Apr 2023 01:28:50 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[matrix]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9999</guid>

					<description><![CDATA[Given a&#160;m x n&#160;binary matrix&#160;mat, find the&#160;0-indexed&#160;position of the row that contains the&#160;maximum&#160;count of&#160;ones,&#160;and the number of ones in that row. In case there are&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a&nbsp;<code>m x n</code>&nbsp;binary matrix&nbsp;<code>mat</code>, find the&nbsp;<strong>0-indexed</strong>&nbsp;position of the row that contains the&nbsp;<strong>maximum</strong>&nbsp;count of&nbsp;<strong>ones,</strong>&nbsp;and the number of ones in that row.</p>



<p>In case there are multiple rows that have the maximum count of ones, the row with the&nbsp;<strong>smallest row number</strong>&nbsp;should be selected.</p>



<p>Return<em>&nbsp;an array containing the index of the row, and the number of ones in it.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,1],[1,0]]
<strong>Output:</strong> [0,1]
<strong>Explanation:</strong> Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)</code>. So, the answer is [0,1]. 
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,0,0],[0,1,1]]
<strong>Output:</strong> [1,2]
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones <code>(2)</code>. So we return its index, <code>1</code>, and the count. So, the answer is [1,2].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> mat = [[0,0],[1,1],[0,0]]
<strong>Output:</strong> [1,2]
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
</pre>



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



<ul class="wp-block-list"><li><code>m == mat.length</code>&nbsp;</li><li><code>n == mat[i].length</code>&nbsp;</li><li><code>1 &lt;= m, n &lt;= 100</code>&nbsp;</li><li><code>mat[i][j]</code>&nbsp;is either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li></ul>



<h2 class="wp-block-heading"><strong>Solution: Counting</strong></h2>



<p>Time complexity: O(m*n)<br>Space complexity: O(1)</p>



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

<pre class="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; rowAndMaximumOnes(vector&lt;vector&lt;int&gt;&gt;&amp; mat) {
    vector&lt;int&gt; ans(2);
    for (int i = 0; i &lt; mat.size(); ++i) {
      int ones = accumulate(begin(mat[i]), end(mat[i]), 0);
      if (ones &gt; ans[1]) {
        ans[0] = i;
        ans[1] = ones;
      }
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2643-row-with-maximum-ones/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 2652. Sum Multiples</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2652-sum-multiples/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2652-sum-multiples/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 28 Apr 2023 04:23:08 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mod]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9992</guid>

					<description><![CDATA[Given a positive integer&#160;n, find the sum of all integers in the range&#160;[1, n]&#160;inclusive&#160;that are divisible by&#160;3,&#160;5, or&#160;7. Return&#160;an integer denoting the sum of all&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Given a positive integer&nbsp;<code>n</code>, find the sum of all integers in the range&nbsp;<code>[1, n]</code>&nbsp;<strong>inclusive</strong>&nbsp;that are divisible by&nbsp;<code>3</code>,&nbsp;<code>5</code>, or&nbsp;<code>7</code>.</p>



<p>Return&nbsp;<em>an integer denoting the sum of all numbers in the given range satisfying&nbsp;the constraint.</em></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 7
<strong>Output:</strong> 21
<strong>Explanation:</strong> Numbers in the range <code>[1, 7]</code> that are divisible by <code>3</code>, <code>5,</code> or <code>7 </code>are <code>3, 5, 6, 7</code>. The sum of these numbers is <code>21</code>.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 10
<strong>Output:</strong> 40
<strong>Explanation:</strong> Numbers in the range <code>[1, 10] that are</code> divisible by <code>3</code>, <code>5,</code> or <code>7</code> are <code>3, 5, 6, 7, 9, 10</code>. The sum of these numbers is 40.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 9
<strong>Output:</strong> 30
<strong>Explanation:</strong> Numbers in the range <code>[1, 9]</code> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code> are <code>3, 5, 6, 7, 9</code>. The sum of these numbers is <code>30</code>.
</pre>



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



<ul class="wp-block-list"><li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li></ul>



<h2 class="wp-block-heading"><strong>Solution: Mod</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="urvanov-syntax-highlighter-plain-tag">// Author: Huahua
class Solution {
public:
  int sumOfMultiples(int n) {
    int ans = 0;
    for (int i = 1; i &lt;= n; ++i) {
      if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0)
        ans += i;
    }
    return ans;
  }
};</pre>
</div></div>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-2652-sum-multiples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
