{"id":441,"date":"2020-09-26T10:41:57","date_gmt":"2020-09-26T02:41:57","guid":{"rendered":"http:\/\/39.96.58.60\/?p=441"},"modified":"2022-10-18T16:39:15","modified_gmt":"2022-10-18T08:39:15","slug":"leetcode-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.yatenglg.cn\/blog\/?p=441","title":{"rendered":"Leetcode 1365. \u6709\u591a\u5c11\u5c0f\u4e8e\u5f53\u524d\u6570\u5b57\u7684\u6570\u5b57"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u5bf9\u4e8e\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20&nbsp;<code>nums[i]<\/code>\uff0c\u8bf7\u4f60\u7edf\u8ba1\u6570\u7ec4\u4e2d\u6bd4\u5b83\u5c0f\u7684\u6240\u6709\u6570\u5b57\u7684\u6570\u76ee\u3002<\/p>\n<p>\u6362\u800c\u8a00\u4e4b\uff0c\u5bf9\u4e8e\u6bcf\u4e2a&nbsp;<code>nums[i]<\/code>&nbsp;\u4f60\u5fc5\u987b\u8ba1\u7b97\u51fa\u6709\u6548\u7684&nbsp;<code>j<\/code>&nbsp;\u7684\u6570\u91cf\uff0c\u5176\u4e2d <code>j<\/code> \u6ee1\u8db3&nbsp;<code>j != i<\/code> <strong>\u4e14<\/strong> <code>nums[j] &lt; nums[i]<\/code>&nbsp;\u3002<\/p>\n<p>\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\u7b54\u6848\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>nums = [8,1,2,2,3]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[4,0,1,1,3]\n\n<strong>\u89e3\u91ca\uff1a<\/strong> \n\n\u5bf9\u4e8e nums[0]=8 \u5b58\u5728\u56db\u4e2a\u6bd4\u5b83\u5c0f\u7684\u6570\u5b57\uff1a\uff081\uff0c2\uff0c2 \u548c 3\uff09\u3002 \n\n\u5bf9\u4e8e nums[1]=1 \u4e0d\u5b58\u5728\u6bd4\u5b83\u5c0f\u7684\u6570\u5b57\u3002\n\n\u5bf9\u4e8e nums[2]=2 \u5b58\u5728\u4e00\u4e2a\u6bd4\u5b83\u5c0f\u7684\u6570\u5b57\uff1a\uff081\uff09\u3002 \n\n\u5bf9\u4e8e nums[3]=2 \u5b58\u5728\u4e00\u4e2a\u6bd4\u5b83\u5c0f\u7684\u6570\u5b57\uff1a\uff081\uff09\u3002 \n\n\u5bf9\u4e8e nums[4]=3 \u5b58\u5728\u4e09\u4e2a\u6bd4\u5b83\u5c0f\u7684\u6570\u5b57\uff1a\uff081\uff0c2 \u548c 2\uff09\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>nums = [6,5,4,8]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[2,1,0,3]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>nums = [7,7,7,7]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[0,0,0,0]\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>2 &lt;= nums.length &lt;= 500<\/code><\/li>\n<li><code>0 &lt;= nums[i] &lt;= 100<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6570\u7ec4\u3001 \u54c8\u5e0c\u8868\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a32 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.89% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.09% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u7edf\u8ba1num\u51fa\u73b0\u7684\u6b21\u6570\n\"\"\"\nclass Solution:\n    def smallerNumbersThanCurrent(self, nums: List[int]) -&gt; List[int]:\n        record1, record2 = [0] * 101, [0] * 101\n\n        for num in nums:\n            record1[num] += 1   # \u7edf\u8ba1\u6bcf\u4e2a\u6570\u51fa\u73b0\u7684\u6b21\u6570\n\n        for i in range(1, 101):\n            record2[i] = record1[i-1] + record2[i-1]    # \u8ba1\u7b97\u6bd4\u5f53\u524d\u6570\u5c0f\u7684\u6570\u7684\u4e2a\u6570\n\n        result = []\n        for num in nums:\n            result += [record2[num]]\n        return result\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a112 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8650.42% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e868.85% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def smallerNumbersThanCurrent(self, nums: List[int]) -&gt; List[int]:\n        record = {} # \u7edf\u8ba1\u6bd4\u5f53\u524d\u6570\u5c0f\u7684\u6570\u4e2a\u6570\n        record_n = defaultdict(int) # \u7edf\u8ba1\u5f53\u524d\u6570\u7684\u51fa\u73b0\u6b21\u6570\n        for num in nums:\n            if num in record:\n                record_n[num] += 1\n                continue\n            record[num] = 0\n\n            for r in record:\n                if r &gt; num:\n                    record[r] += 1\n                elif r &lt; num:\n                    record[num] += 1\n\n        result = []\n        for num in nums:\n            n = record[num] # \u6700\u7ec8\u7ed3\u679c\uff0c\u5c0f\u4e8e\u5f53\u524d\u6570\u7684\u4e2a\u6570+\u6bcf\u4e2a\u5c0f\u4e8e\u5f53\u524d\u6570\u7684\u6570\u7684\u51fa\u73b0\u6b21\u6570\n            for rn in record_n:\n                if rn &lt; num:\n                    n += record_n[rn]\n            result += [n]\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4&nbsp;nums\uff0c\u5bf9\u4e8e\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20&nbsp;nums[i]\uff0c\u8bf7\u4f60\u7edf\u8ba1\u6570\u7ec4\u4e2d\u6bd4\u5b83\u5c0f\u7684\u6240\u6709\u6570\u5b57\u7684&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/441"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=441"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":442,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions\/442"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}