{"id":331,"date":"2025-07-01T13:59:37","date_gmt":"2025-07-01T05:59:37","guid":{"rendered":"http:\/\/getzs.online\/?p=331"},"modified":"2025-07-01T13:59:37","modified_gmt":"2025-07-01T05:59:37","slug":"135-%e5%88%86%e5%8f%91%e7%b3%96%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/07\/01\/135-%e5%88%86%e5%8f%91%e7%b3%96%e6%9e%9c\/","title":{"rendered":"135. \u5206\u53d1\u7cd6\u679c"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/candy\/\">135. \u5206\u53d1\u7cd6\u679c<\/a><\/p>\n\n\n\n<p><code>n<\/code>&nbsp;\u4e2a\u5b69\u5b50\u7ad9\u6210\u4e00\u6392\u3002\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>ratings<\/code>&nbsp;\u8868\u793a\u6bcf\u4e2a\u5b69\u5b50\u7684\u8bc4\u5206\u3002<\/p>\n\n\n\n<p>\u4f60\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u8981\u6c42\uff0c\u7ed9\u8fd9\u4e9b\u5b69\u5b50\u5206\u53d1\u7cd6\u679c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2a\u5b69\u5b50\u81f3\u5c11\u5206\u914d\u5230\u00a0<code>1<\/code>\u00a0\u4e2a\u7cd6\u679c\u3002<\/li>\n\n\n\n<li>\u76f8\u90bb\u4e24\u4e2a\u5b69\u5b50\u8bc4\u5206\u66f4\u9ad8\u7684\u5b69\u5b50\u4f1a\u83b7\u5f97\u66f4\u591a\u7684\u7cd6\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8bf7\u4f60\u7ed9\u6bcf\u4e2a\u5b69\u5b50\u5206\u53d1\u7cd6\u679c\uff0c\u8ba1\u7b97\u5e76\u8fd4\u56de\u9700\u8981\u51c6\u5907\u7684&nbsp;<strong>\u6700\u5c11\u7cd6\u679c\u6570\u76ee<\/strong>&nbsp;\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>ratings = [1,0,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>5\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4f60\u53ef\u4ee5\u5206\u522b\u7ed9\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e8c\u4e2a\u3001\u7b2c\u4e09\u4e2a\u5b69\u5b50\u5206\u53d1 2\u30011\u30012 \u9897\u7cd6\u679c\u3002\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>ratings = [1,2,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4f60\u53ef\u4ee5\u5206\u522b\u7ed9\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e8c\u4e2a\u3001\u7b2c\u4e09\u4e2a\u5b69\u5b50\u5206\u53d1 1\u30012\u30011 \u9897\u7cd6\u679c\u3002\n     \u7b2c\u4e09\u4e2a\u5b69\u5b50\u53ea\u5f97\u5230 1 \u9897\u7cd6\u679c\uff0c\u8fd9\u6ee1\u8db3\u9898\u9762\u4e2d\u7684\u4e24\u4e2a\u6761\u4ef6\u3002<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>n == ratings.length<\/code><\/li>\n\n\n\n<li><code>1 &lt;= n &lt;= 2 * 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>0 &lt;= ratings[i] &lt;= 2 * 10<sup>4<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    public int candy(int&#91;] ratings) {\n        return method1(ratings, ratings.length);\n    }\n\n    \/**\n     * \u8d2a\u5fc3\u7b97\u6cd5\n     * 7 ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method1(int&#91;] ratings, int n) {\n        int&#91;]nums = new int&#91;n];\n        \/\/ \u521d\u59cb\u5316\u8d4b\u503c\n        Arrays.fill(nums, 1);\n        \/\/ \u4ece\u5de6\u5230\u53f3\u5224\u65ad\uff0c\u53f3\u8fb9\u5927\u4e8e\u5de6\u8fb9\uff0c\u5219\u53f3\u8fb9\u7684\u7cd6\u679c\u6570\u6bd4\u5de6\u8fb9\u591a1\n        for (int i = 1 ; i &lt; n ; i++) {\n            if (ratings&#91;i] > ratings&#91;i - 1] &amp;&amp; nums&#91;i] &lt;= nums&#91;i - 1]) {\n                nums&#91;i] = nums&#91;i - 1] + 1;\n            }\n        }\n        \/\/ \u540c\u7406\n        for (int i = n - 2 ; i >= 0 ; i--) {\n            if (ratings&#91;i] > ratings&#91;i + 1] &amp;&amp; nums&#91;i] &lt;= nums&#91;i + 1]) {\n                nums&#91;i] = nums&#91;i + 1] + 1;\n            }\n        }\n        return Arrays.stream(nums).sum();\n    }\n\n    \/**\n     * \u8d2a\u5fc3\u7b97\u6cd5\n     * 2 ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method2(int&#91;] ratings, int n) {\n        int ans = n;\n        for (int i = 0; i &lt; n; i++) {\n            \/\/ \u8d77\u70b9\n            int start = i > 0 &amp;&amp; ratings&#91;i - 1] &lt; ratings&#91;i] ? i - 1 : i;\n            \/\/ \u9012\u589e\n            while (i + 1 &lt; n &amp;&amp; ratings&#91;i] &lt; ratings&#91;i + 1]) {\n                i++;\n            }\n            \/\/ \u5cf0\u9876\n            int top = i;\n            \/\/ \u9012\u51cf\n            while (i + 1 &lt; n &amp;&amp; ratings&#91;i] > ratings&#91;i + 1]) {\n                i++;\n            }\n            \/\/ \u9012\u589e\u533a\u95f4\n            int inc = top - start;\n            \/\/ \u9012\u51cf\u533a\u95f4\n            int dec = i - top;\n            \/\/ \u5cf0\u9876\u7684\u503c\u4e3a\u9012\u589e\u6216\u9012\u589e\u7684\u6700\u5927\u957f\u5ea6\uff0c\u5176\u4f59\u4f9d\u6b21\u9012\u51cf\n            ans += (inc * (inc - 1) + dec * (dec - 1)) \/ 2 + Math.max(inc, dec);\n        }\n        return ans;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>135. \u5206\u53d1\u7cd6\u679c n&nbsp;\u4e2a\u5b69\u5b50\u7ad9\u6210\u4e00\u6392\u3002\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;ratings&nbsp;\u8868\u793a\u6bcf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,11],"tags":[],"class_list":["post-331","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=331"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/331\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/331\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}