{"id":1547,"date":"2020-05-07T17:29:35","date_gmt":"2020-05-07T09:29:35","guid":{"rendered":"http:\/\/149.129.74.185\/?p=1547"},"modified":"2020-05-07T17:32:55","modified_gmt":"2020-05-07T09:32:55","slug":"leetcode-%e7%ae%80%e5%8d%95-1365-%e6%9c%89%e5%a4%9a%e5%b0%91%e5%b0%8f%e4%ba%8e%e5%bd%93%e5%89%8d%e6%95%b0%e5%ad%97%e7%9a%84%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"http:\/\/www.magicmipamipa.com\/?p=1547","title":{"rendered":"[Leetcode] (\u7b80\u5355) 1365.\u6709\u591a\u5c11\u5c0f\u4e8e\u5f53\u524d\u6570\u5b57\u7684\u6570\u5b57"},"content":{"rendered":"<div class=\"notranslate\">\n<p><span style=\"font-family: 'courier new', courier, monospace;\">Given the array <code>nums<\/code>, for each <code>nums[i]<\/code> find out how many numbers in the array are smaller than it. That is, for each <code>nums[i]<\/code> you have to count the number of valid <code>j's<\/code>\u00a0such that\u00a0<code>j != i<\/code><strong>and<\/strong><code>nums[j] &lt; nums[i]<\/code>.<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier, monospace;\">Return the answer in an array.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Example 1:<\/strong><\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Input:<\/strong><\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"> nums = [8,1,2,2,3]\r\n<\/span><\/pre>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Output:<\/strong><\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"> [4,0,1,1,3]\r\n<\/span><\/pre>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Explanation:<\/strong><\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"> \r\nFor nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). \r\nFor nums[1]=1 does not exist any smaller number than it.\r\nFor nums[2]=2 there exist one smaller number than it (1). \r\nFor nums[3]=2 there exist one smaller number than it (1). \r\nFor nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).\r\n<\/span><\/pre>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Example 2:<\/strong><\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Input:<\/strong><\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"> nums = [6,5,4,8]\r\n<\/span><\/pre>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Output:<\/strong><\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"> [2,1,0,3]\r\n<\/span><\/pre>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Example 3:<\/strong><\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Input:<\/strong><\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"> nums = [7,7,7,7]\r\n<\/span><\/pre>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Output:<\/strong><\/span><\/p>\n<pre><span style=\"font-family: 'courier new', courier, monospace;\"> [0,0,0,0]\r\n<\/span><\/pre>\n<p><span style=\"font-family: 'courier new', courier, monospace;\"><strong>Constraints:<\/strong><\/span><\/p>\n<ul>\n<li><span style=\"font-family: 'courier new', courier, monospace;\"><code>2 &lt;= nums.length &lt;= 500<\/code><\/span><\/li>\n<li><span style=\"font-family: 'courier new', courier, monospace;\"><code>0 &lt;= nums[i] &lt;= 100<\/code><\/span><\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528\u6876\u6392\u5e8f\uff0c\u5728nums\u7684\u5927\u5c0f\u8303\u56f4\u5185\u53ef\u4ee5\u8fbe\u5230\u5e38\u6570\u4e58N\u7684\u590d\u6742\u5ea6\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">class Solution {\r\npublic:\r\n    vector&lt;int&gt; smallerNumbersThanCurrent(vector&lt;int&gt;&amp; nums) {\r\n        int a[101] = {0};\r\n        vector&lt;int&gt; b;\r\n        for( auto num : nums ){\r\n            a[num]++;\r\n        }\r\n        for( int i = 1 ; i &lt; 101 ; i ++ ){\r\n            a[i] = a[i] + a[i-1];\r\n        }\r\n        for( int i = 0 ; i &lt; nums.size() ; i ++ ){\r\n            if( nums[i] == 0 ) b.push_back(0);\r\n            else    b.push_back(a[nums[i]-1]);\r\n        }\r\n        return b;\r\n    }\r\n};<\/pre>\n<p>\u4f18\u53161\uff1a\u4f7f\u7528\u6876\u6392\u5e8f\u800c\u4e0d\u4f7f\u7528\u66b4\u529b\u6392\u5e8f\u3002<\/p>\n<p>\u4f18\u53162\uff1a\u4f7f\u7528\u5355\u4e2a\u6570\u7ec4\u5b58\u50a8\u6876\u5185\u5bb9\u4e0e\u524d\u5e8f\u5185\u5bb9\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the array nums, for each nums[i] find out ho [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,16],"tags":[],"_links":{"self":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts\/1547"}],"collection":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1547"}],"version-history":[{"count":1,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts\/1547\/revisions"}],"predecessor-version":[{"id":1548,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts\/1547\/revisions\/1548"}],"wp:attachment":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1547"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}