{"id":6395,"date":"2020-03-02T00:52:29","date_gmt":"2020-03-02T08:52:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6395"},"modified":"2020-03-02T00:52:30","modified_gmt":"2020-03-02T08:52:30","slug":"%e8%8a%b1%e8%8a%b1%e9%85%b1-leetcode-1365-how-many-numbers-are-smaller-than-the-current-number","status":"publish","type":"post","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\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1365. How Many Numbers Are Smaller Than the Current Number"},"content":{"rendered":"\n<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>\n\n\n\n<p>Return the answer in an array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [8,1,2,2,3]\n<strong>Output:<\/strong> [4,0,1,1,3]\n<strong>Explanation:<\/strong> \nFor nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). \nFor nums[1]=1 does not exist any smaller number than it.\nFor nums[2]=2 there exist one smaller number than it (1). \nFor nums[3]=2 there exist one smaller number than it (1). \nFor nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [6,5,4,8]\n<strong>Output:<\/strong> [2,1,0,3]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [7,7,7,7]\n<strong>Output:<\/strong> [0,0,0,0]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 500<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> smallerNumbersThanCurrent(vector<int>& nums) {\n    const int n = nums.size();\n    vector<int> ans(n);\n    for (int i = 0; i < n; ++i)\n      for (int j = 0; j < n; ++j)\n        if (i != j &#038;&#038; nums[j] < nums[i]) ++ans[i];          \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Sort + Binary Search<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> smallerNumbersThanCurrent(vector<int>& nums) {\n    const int n = nums.size();\n    vector<int> s(nums);\n    sort(begin(s), end(s));    \n    vector<int> ans(n);\n    for (int i = 0; i < n; ++i)\n      ans[i] = distance(begin(s), lower_bound(begin(s), end(s), nums[i]));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 3: Cumulative frequency<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(101)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> smallerNumbersThanCurrent(vector<int>& nums) {\n    vector<int> f(101);\n    for (int x : nums) ++f[x];\n    partial_sum(begin(f), end(f), begin(f));    \n    for (int& x : nums) \n      x = x == 0 ? 0 : f[x - 1];\n    return nums;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the array&nbsp;nums, for each&nbsp;nums[i]&nbsp;find out how many numbers in the array are smaller than it. That is, for each&nbsp;nums[i]&nbsp;you have to count the number&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,8,222,565,23],"class_list":["post-6395","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-counting","tag-easy","tag-smaller","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6395"}],"version-history":[{"count":1,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6395\/revisions"}],"predecessor-version":[{"id":6396,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6395\/revisions\/6396"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}