{"id":7450,"date":"2023-04-24T16:00:05","date_gmt":"2023-04-24T08:00:05","guid":{"rendered":"https:\/\/www.5x44.cn\/?p=7450"},"modified":"2023-04-25T23:00:46","modified_gmt":"2023-04-25T15:00:46","slug":"%e7%ae%97%e6%b3%95%e9%a2%98%e5%b0%8f%e6%9c%8b%e5%8f%8b%e5%88%86%e7%b3%96%e9%97%ae%e9%a2%98-2","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=7450","title":{"rendered":"[\u7b97\u6cd5\u9898]\u5c0f\u670b\u53cb\u5206\u7cd6\u95ee\u9898(\u76f8\u540c\u7684\u76f8\u90bb\u6570\u636e\u53ef\u4ee5\u62e5\u6709\u4e0d\u540c\u7684\u7cd6)"},"content":{"rendered":"\n<p>\u95ee\u9898\uff1a\u73b0\u5728\u6709n\u4e2a\u5c0f\u670b\u53cb\u5750\u6210\u4e00\u6392\uff0c\u6bcf\u4e2a\u5c0f\u670b\u53cb\u90fd\u6709\u4e00\u4e2a\u7ade\u8d5b\u5f97\u5206\uff0c\u73b0\u5728\u8981\u6839\u636e\u7ade\u8d5b\u5f97\u5206\u7ed9\u5c0f\u670b\u53cb\u5206\u7cd6\uff0c\u8981\u6c42\u662f\u76f8\u90bb\u5750\u7740\u7684\u5c0f\u670b\u53cb\uff0c\u5206\u6570\u9ad8\u7684\u5c0f\u670b\u53cb\u7684\u7cd6\u8981\u6bd4\u5206\u6570\u4f4e\u7684\u5c0f\u670b\u53cb\u7cd6\u591a\uff08\u5c0f\u670b\u53cb\u662f\u968f\u673a\u5750\u7684\uff0c\u4e5f\u5c31\u662f\u5206\u6570\u6ca1\u6392\u5e8f\uff09\uff0c\u76f8\u90bb\u7684\u4e24\u4e2a\u5c0f\u670b\u53cb\u5206\u6570\u5982\u679c\u76f8\u540c\uff0c\u5206\u914d\u7cd6\u7684\u6570\u91cf\u53ef\u4ee5\u4e0d\u540c\uff0c\u73b0\u5728\u7a0b\u5e8f\u8981\u7ed9\u51fa\u6700\u5c11\u9700\u8981\u591a\u5c11\u7cd6\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u7f51\u53cb\u7684\u65b9\u6848\uff0c\u6bd4\u6211\u81ea\u5df1\u5199\u7684\u65b9\u6848\u7b80\u5355\u4e86\u5f88\u591a\u4e86\uff0c\u770b\u4e0a\u53bb\u5c31\u5f88\u6e05\u723d\u3002\u3002\u3002\u800c\u4e14\u6ca1\u7528\u9012\u5f52\uff0c\u4e24\u6b21\u8fed\u4ee3\u5c31\u53ef\u4ee5\u5b8c\u6210<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\ndef candies_1(n, arr):\n    N = n\n\n    ratings = arr\n    candies = &#91;1 for i in range(N)]\n\n\n    for i in range(len(ratings) - 1):\n        le = ratings&#91;i]\n        ri = ratings&#91;i + 1]\n\n        if le &lt; ri:\n            candies&#91;i + 1] = candies&#91;i] + 1\n        elif le &gt; ri:\n            if candies&#91;i] == 1:\n                for e in range(i, -1, -1):\n                    if ratings&#91;e] &gt; ratings&#91;e + 1] and candies&#91;e] &lt;= candies&#91;e + 1]:\n                        candies&#91;e] = candies&#91;e+1]+1\n                    else:\n                        break\n            else:\n                candies&#91;i + 1] = 1  # candies&#91;i] - 1 #max(1, prev-1)\n\n    return sum(candies)\n\nif __name__ == '__main__':\n\n    n = 16387\n    arr = &#91;]\n    with open(r'E:\\practices\\python\\test_data.json', 'r') as f:\n        data = f.read()\n        arr = json.loads(data)\n    print(candies_1(n, arr))<\/code><\/pre>\n\n\n\n<p>\u4e09\u5929\u4e09\u591c\u554a\uff01\u7ec8\u4e8e\u81ea\u5df1\u7528\u9012\u5f52\u5199\u51fa\u6765\u4e86\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def candies(n, arr):\n    arr = &#91;&#91;x, 1] for x in arr]\n    for i in range(n):\n        get_candy_num_v2(i, arr)\n    return sum(&#91;x&#91;1] for x in arr])\n\n#\u8fd4\u56de\u5f53\u524d\u5c0f\u670b\u53cb\u5e94\u5f97\u7cd6\u7684\u6570\u91cf\ndef get_candy_num_v2(n,arr):\n    #\u7528\u4e8e\u907f\u514d\u8d85\u51fa\u961f\u5217\u7ed3\u5c3e\u7684\u8fb9\u754c\n    if n==len(arr)-1:\n        return arr&#91;n]&#91;1]\n    #\u5982\u679c\u5f53\u524d\u5c0f\u670b\u53cb\u7684\u7ade\u8d5b\u5206\u6570\u6bd4\u4e0b\u4e00\u4e2a\u5c0f\u670b\u53cb\u7684\u7ade\u8d5b\u5206\u6570\u591a\u3002\n    if arr&#91;n]&#91;0] &gt; arr&#91;n+1]&#91;0]:\n        #n\u4ee3\u8868\u5f53\u524d\u5c0f\u670b\u53cb\uff1b\u5982\u679c\u5f53\u524d\u5c0f\u670b\u53cb\u7684\u7cd6\u6bd4\u4e0b\u4e00\u4e2a\u5c0f\u670b\u53cb\u7684\u7cd6\u591a\uff0c\u90a3\u4e48\u4e0d\u589e\u51cf\u5b83\u7cd6\u7684\u6570\u91cf\uff0c\u5426\u5219\u8981\u6bd4\u4e0b\u4e00\u4e2a\u5c0f\u670b\u53cb\u7684\u7cd6\u591a1\u4e2a\u3002\n        <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">arr&#91;n]&#91;1] = arr&#91;n]&#91;1] if arr&#91;n]&#91;1]&gt;get_candy_num_v2(n+1,arr) else (arr&#91;n+1]&#91;1]+1)<\/mark><\/strong>\n    elif arr&#91;n]&#91;0] &lt; arr&#91;n+1]&#91;0]:\n        #\u8fd9\u91cc\u7684\u505a\u6cd5\u662f\u4e3a\u4e86\u907f\u514d\u6d4b\u8bd5arr&#91;n-1]\u65f6\u4f1a\u5728n=0\u65f6\u6ea2\u51fa\u961f\u5217\u8303\u56f4\uff0c\u51cf\u5c11\u8fb9\u754c\u63a7\u5236\u7684\u95ee\u9898\u3002\n        arr&#91;n+1]&#91;1]=arr&#91;n]&#91;1]+1\n\n    return arr&#91;n]&#91;1]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6d4b\u8bd5\u6570\u636e\uff1a\nn=7\narr=[2,6,5,5,3,4,1]\n\u7ed3\u679c\uff1a10<\/pre>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-6829f4fb-000d-4f0f-bfb2-7ed200e5cf49\" href=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/04\/test_data.7z\">test_data<\/a><a href=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2023\/04\/test_data.7z\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-6829f4fb-000d-4f0f-bfb2-7ed200e5cf49\">\u6587\u4ef6\u540d\u5c31\u662f\u6d4b\u8bd5\u7ed3\u679c<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\uff1a\u73b0\u5728\u6709n\u4e2a\u5c0f\u670b\u53cb\u5750\u6210\u4e00\u6392\uff0c\u6bcf\u4e2a\u5c0f\u670b\u53cb\u90fd\u6709\u4e00\u4e2a\u7ade\u8d5b\u5f97\u5206\uff0c\u73b0\u5728\u8981\u6839\u636e\u7ade\u8d5b\u5f97\u5206\u7ed9\u5c0f\u670b\u53cb\u5206\u7cd6\uff0c\u8981\u6c42\u662f\u76f8\u90bb\u5750\u7740\u7684\u5c0f\u670b\u53cb\uff0c\u5206\u6570\u9ad8\u7684\u5c0f\u670b\u53cb\u7684\u7cd6\u8981\u6bd4\u5206\u6570\u4f4e\u7684\u5c0f\u670b\u53cb\u7cd6\u591a\uff08\u5c0f&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=7450\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-7450","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7450"}],"version-history":[{"count":8,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7450\/revisions"}],"predecessor-version":[{"id":7462,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/7450\/revisions\/7462"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}