<?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>smaller Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/smaller/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/smaller/</link>
	<description></description>
	<lastBuildDate>Mon, 02 Mar 2020 08:52:30 +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>smaller Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/smaller/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1365. How Many Numbers Are Smaller Than the Current Number</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-1365-how-many-numbers-are-smaller-than-the-current-number/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-1365-how-many-numbers-are-smaller-than-the-current-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 02 Mar 2020 08:52:29 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[counting]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[smaller]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6395</guid>

					<description><![CDATA[<p>Given the array&#160;nums, for each&#160;nums[i]&#160;find out how many numbers in the array are smaller than it. That is, for each&#160;nums[i]&#160;you have to count the number&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-1365-how-many-numbers-are-smaller-than-the-current-number/">花花酱 LeetCode 1365. How Many Numbers Are Smaller Than the Current Number</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&nbsp;<code>nums</code>, for each&nbsp;<code>nums[i]</code>&nbsp;find out how many numbers in the array are smaller than it. That is, for each&nbsp;<code>nums[i]</code>&nbsp;you have to count the number of valid&nbsp;<code>j's</code>&nbsp;such that&nbsp;<code>j != i</code>&nbsp;<strong>and</strong>&nbsp;<code>nums[j] &lt; nums[i]</code>.</p>



<p>Return the answer in an array.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [8,1,2,2,3]
<strong>Output:</strong> [4,0,1,1,3]
<strong>Explanation:</strong> 
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). 
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1). 
For nums[3]=2 there exist one smaller number than it (1). 
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [6,5,4,8]
<strong>Output:</strong> [2,1,0,3]
</pre>



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



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



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



<ul><li><code>2 &lt;= nums.length &lt;= 500</code></li><li><code>0 &lt;= nums[i] &lt;= 100</code></li></ul>



<h2><strong>Solution 1: Brute Force</strong></h2>



<p>Time complexity: O(n^2)<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:
  vector&lt;int&gt; smallerNumbersThanCurrent(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    vector&lt;int&gt; ans(n);
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (i != j &amp;&amp; nums[j] &lt; nums[i]) ++ans[i];          
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Sort + Binary Search</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; smallerNumbersThanCurrent(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    vector&lt;int&gt; s(nums);
    sort(begin(s), end(s));    
    vector&lt;int&gt; ans(n);
    for (int i = 0; i &lt; n; ++i)
      ans[i] = distance(begin(s), lower_bound(begin(s), end(s), nums[i]));
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 3: Cumulative frequency</strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; smallerNumbersThanCurrent(vector&lt;int&gt;&amp; nums) {
    vector&lt;int&gt; f(101);
    for (int x : nums) ++f[x];
    partial_sum(begin(f), end(f), begin(f));    
    for (int&amp; x : nums) 
      x = x == 0 ? 0 : f[x - 1];
    return nums;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-1365-how-many-numbers-are-smaller-than-the-current-number/">花花酱 LeetCode 1365. How Many Numbers Are Smaller Than the Current Number</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/%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-1365-how-many-numbers-are-smaller-than-the-current-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
