{"id":200419,"date":"2020-11-14T08:07:04","date_gmt":"2020-11-14T00:07:04","guid":{"rendered":"http:\/\/4563.org\/?p=200419"},"modified":"2020-11-14T08:07:04","modified_gmt":"2020-11-14T00:07:04","slug":"%e5%be%ae%e8%bd%af%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%8c%e7%95%a5%e5%a4%8d%e6%9d%82","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=200419","title":{"rendered":"\u5fae\u8f6f\u9762\u8bd5\u9898\uff0c\u7565\u590d\u6742"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5fae\u8f6f\u9762\u8bd5\u9898\uff0c\u7565\u590d\u6742               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7ed9\u51fa\u4e00\u4e2a\u53ef\u80fd\u5305\u542b\u91cd\u590d\u7684\u6574\u6570\u6570\u7ec4\uff0c\u548c\u4e00\u4e2a\u5927\u5c0f\u4e3a k \u7684\u6ed1\u52a8\u7a97\u53e3, \u4ece\u5de6\u5230\u53f3\u5728\u6570\u7ec4\u4e2d\u6ed1\u52a8\u8fd9\u4e2a\u7a97\u53e3\uff0c\u627e\u5230\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u7a97\u53e3\u5185\u7684\u6700\u5927\u503c\u3002<\/p>\n<p>\u9898\u76ee\u94fe\u63a5<\/p>\n<h2>\u6837\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165: [1,2,7,7,8] 3 \u8f93\u51fa: [7,7,8]  \u89e3\u91ca\uff1a \u6700\u5f00\u59cb\uff0c\u7a97\u53e3\u7684\u72b6\u6001\u5982\u4e0b\uff1a`[|1, 2 ,7| ,7 , 8]`, \u6700\u5927\u503c\u4e3a `7`; \u7136\u540e\u7a97\u53e3\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff1a`[1, |2, 7, 7|, 8]`, \u6700\u5927\u503c\u4e3a `7`; \u6700\u540e\u7a97\u53e3\u518d\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff1a`[1, 2, |7, 7, 8|]`, \u6700\u5927\u503c\u4e3a `8`. <\/code><\/pre>\n<h2>\u6837\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165: [1,2,3,1,2,3] 5 \u8f93\u51fa: [3,3]  \u89e3\u91ca: \u6700\u5f00\u59cb\uff0c\u7a97\u53e3\u7684\u72b6\u6001\u5982\u4e0b\uff1a `[|1,2,3,1,2 | ,3]` , \u6700\u5927\u503c\u4e3a`3`; \u7136\u540e\u7a97\u53e3\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d.`[1, |2,3,1,2,3]`, \u6700\u5927\u503c\u4e3a `3`; <\/code><\/pre>\n<h2>\u7b97\u6cd5\uff1a\u5355\u8c03\u961f\u5217<\/h2>\n<ul>\n<li>\u5355\u8c03\u961f\u5217\u4e2d\u7684\u5143\u7d20\u662f\u4e25\u683c\u5355\u8c03\u7684\u3002\u6211\u4eec\u5728\u6c42\u89e3\u8fd9\u4e2a\u95ee\u9898\u7684\u65f6\u5019\u9700\u8981\u7ef4\u62a4\u4ed6\u7684\u5355\u8c03\u6027\u3002<\/li>\n<li>\u961f\u9996\u5143\u7d20\u5373\u4e3a\u5f53\u524d\u4f4d\u7f6e\u7684\u6700\u5927\u503c\u3002\u5047\u8bbe\u8981\u6c42\u6ed1\u52a8\u7a97\u53e3\u4e2d\u7684\u6700\u5927\u503c\u3002\u6211\u4eec\u5c31\u9700\u8981\u786e\u4fdd\u6ed1\u52a8\u7a97\u53e3\u4e2d\u7684\u5143\u7d20\u4ece\u961f\u9996\u5230\u961f\u5c3e\u662f\u9012\u51cf\u7684\u3002<\/li>\n<li>\u6bcf\u6ed1\u52a8\u4e00\u6b21\u5c31\u5224\u65ad\u5f53\u524d\u5143\u7d20\u548c\u961f\u5c3e\u5143\u7d20\u7684\u5173\u7cfb\uff0c\u5982\u679c\u653e\u5165\u961f\u5c3e\u6ee1\u8db3\u5355\u8c03\u9012\u51cf\uff0c\u90a3\u4e48\u653e\u5165\u5373\u53ef\uff1b\u5982\u679c\u653e\u5165\u4e0d\u6ee1\u8db3\uff0c\u5c31\u9700\u8981\u5220\u9664\u961f\u5c3e\u5143\u7d20\u76f4\u5230\u653e\u5165\u5f53\u524d\u5143\u7d20\u4e4b\u540e\u6ee1\u8db3\u961f\u5217\u5355\u8c03\u9012\u51cf\u3002\u540c\u65f6\u8981\u786e\u4fdd\u5df2\u7ecf\u51fa\u7a97\u53e3\u7684\u6700\u5927\u503c\uff08\u961f\u9996\u5143\u7d20\uff09\u88ab\u5220\u9664\u6389\u3002<\/li>\n<\/ul>\n<h2>\u6ce8\u610f\u4e8b\u9879\uff1a<\/h2>\n<p>\u6211\u4eec\u5728\u672c\u9898\u4e2d \u5f80\u5355\u8c03\u961f\u5217\u91cc\u538b\u5165\u7684\u662f\u5143\u7d20\u5bf9\u5e94\u7684\u4e0b\u6807\uff0c\u56e0\u4e3a\u6211\u4eec\u8981\u901a\u8fc7\u4e0b\u6807\u6765\u5224\u65ad\u8be5\u5143\u7d20\u6709\u6ca1\u6709\u79bb\u5f00\u4e86\u6ed1\u52a8\u7a97\u53e3\uff0c\u5982\u679c\u79bb\u5f00\u4e86\u6ed1\u52a8\u7a97\u53e3\u5c31\u5f97\u4ece\u961f\u5217\u91cc\u7ed9\u5220\u9664\u4e86<\/p>\n<h2>\u6d41\u7a0b<\/h2>\n<p>\u5e8f\u5217 a \u662f 1 2 7 7 6 5 3 \u6ed1\u52a8\u7a97\u53e3\u7684\u5927\u5c0f\u662f 3 \u904d\u5386\u4e00\u904d for i in 0\uff1a7<\/p>\n<ul>\n<li>i==0 \u961f\u5217\u91cc\u6ca1\u6709\u5143\u7d20\uff0c\u76f4\u63a5\u6dfb\u52a0\uff08 value=1,index=0 \uff09 value \u662f\u6743\u503c\uff0cindex \u662f\u4e0b\u6807\uff08\u8fd9\u91cc\u4e3a\u4e86\u8868\u8fbe\u6e05\u6670\uff0c\u5c31\u628a\u6743\u503c\u548c\u4e0b\u6807\u90fd\u6ce8\u660e\u51fa\u6765\u4e86\uff09 \u961f\u5217\u91cc (1,0)<\/li>\n<li>i==1 a[1]\u6bd4\u961f\u5c3e\u5143\u7d20\u5927\uff0c\u6dfb\u52a0 a[1]\u540e\u4e0d\u6ee1\u8db3\u5355\u8c03\u9012\u51cf\u6027\u8d28\uff0c\u6240\u4ee5\u9700\u8981\u628a\u961f\u5c3e\u7ed9\u5f39\u51fa\uff0ca[1]\u6bd4\u961f\u5c3e\u5927\uff0c\u800c\u4e14\u961f\u5c3e\u5143\u7d20\u80af\u5b9a\u5728 a[1]\u7684\u5de6\u9762\uff08\u56e0\u4e3a\u961f\u5c3e\u5143\u7d20\u5148\u538b\u5165\u7684\uff0c\u5b83\u7684\u4e0b\u6807\u80af\u5b9a\u5c0f\uff09\uff0c\u800c\u4e14\u961f\u5c3e\u5143\u7d20\u8fd8\u6ca1 a[1]\u5927\uff0c\u5bf9\u4e8e\u540e\u9762\u7684\u7a97\u53e3\uff0c\u961f\u5c3e\u5143\u7d20\u80af\u5b9a\u4e0d\u662f\u6700\u5927\u503c\u4e86\uff08\u6211\u4eec\u5df2\u7ecf\u627e\u5230\u4e86 a[1]\u6bd4\uff0c\u800c\u4e14\u961f\u5c3e\u5728\u7a97\u53e3\u91cc\u9762\uff0ca[1]\u80af\u5b9a\u4e5f\u5728\u7a97\u53e3\u91cc\u9762\uff0c\u56e0\u4e3a a[1]\u7684\u4e0b\u6807\u66f4\u5927\uff09\n<ul>\n<li>\u5f39\u6389\u961f\u5c3e\u540e\uff0c\u961f\u5217\u4e3a\u7a7a<\/li>\n<li>\u538b\u5165(2,1)<\/li>\n<li>\u961f\u5217\u91cc (2,1)<\/li>\n<\/ul>\n<\/li>\n<li>i==2 a[2]\u6bd4\u961f\u5c3e\u5143\u7d20\u5927\uff0c\u5f39\u6389\u961f\u5c3e\uff0c\u538b\u5165(7,2) \u961f\u5217\u91cc (7,2) \u8fd9\u91cc i&gt;=k-1 \u4e86 \uff0c\u5f53\u524d\u961f\u9996\u4e3a\u5f53\u524d[0,2]\u8fd9\u4e2a\u6ed1\u52a8\u7a97\u53e3\u7684\u7b54\u6848\uff0c\u8fd9\u4e2a\u7a97\u53e3\u7b54\u6848\u662f 7<\/li>\n<li>i==3 a[3]\u4e0d\u6bd4\u961f\u5c3e\u5143\u7d20\u5927\uff0c\u4e0d\u7528\u5f39\u51fa\u961f\u5c3e \uff0c\u538b\u5165\u65b0\u5143\u7d20 \u961f\u5217\u91cc (7,2) (7,3) \u7edf\u8ba1[1,3]\u6ed1\u52a8\u7a97\u53e3\u7684\u7b54\u6848\uff0c\u4e5f\u662f 7<\/li>\n<li>i==4 a[4]\u4e0d\u6bd4\u961f\u5c3e\u5143\u7d20\u5927\uff0c\u4e0d\u7528\u5f39\u51fa\u961f\u5c3e\uff0c\u538b\u5165\u65b0\u5143\u7d20 \u961f\u5217\u91cc (7,2) (7,3) \uff08 6\uff0c4 \uff09 \u7edf\u8ba1[2\uff0c4]\u6ed1\u52a8\u7a97\u53e3\u7684\u7b54\u6848\uff0c\u4e5f\u662f 7<\/li>\n<li>i==5 a[5]\u4e0d\u6bd4\u961f\u5c3e\u5143\u7d20\u5927\uff0c\u4e0d\u7528\u5f39\u51fa\u961f\u5c3e\uff0c\u538b\u5165\u65b0\u5143\u7d20 \u961f\u5217\u91cc (7,2) (7,3)\uff08 6\uff0c4 \uff09\uff08 5\uff0c5 \uff09 \u8fd9\u65f6\u5019 i \u548c\u961f\u9996\u4f4d\u7f6e\u5dee\u5df2\u7ecf\u8d85\u8fc7\u4e86\u6ed1\u52a8\u7a97\u53e3\u957f\u5ea6\uff0c\u5f39\u51fa\u961f\u9996 \u961f\u5217\u91cc (7,3) \uff08 6\uff0c4 \uff09\uff08 5\uff0c5 \uff09 \u8fd9\u4e2a\u7a97\u53e3\u7b54\u6848\u4e5f\u4e3a 7<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6bcf\u4e2a\u5143\u7d20\u8fdb\u961f\u51fa\u961f\u90fd\u53ea\u6709\u4e00\u6b21\uff0c\u6240\u4ee5\u590d\u6742\u5ea6\u662f O(N)\u7684 \u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u961f\u5217\u91cc\u5143\u7d20\u6700\u591a\u4e3a N \u4e2a,\u6240\u4ee5\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u662f O(N)<\/p>\n<pre><code>public class Solution {     \/**      * @param nums: A list of integers.      * @param k: An integer      * @return: The maximum number inside the window at each moving.      *\/     public List&lt;Integer&gt; maxSlidingWindow(int[] nums, int k) {         \/\/ write your code here         \/\/ \u7b54\u6848\u6570\u7ec4\u548c\u5355\u8c03\u961f\u5217         List&lt;Integer&gt; ans = new ArrayList&lt;Integer&gt;();         Deque&lt;Integer&gt; qmax = new LinkedList&lt;Integer&gt;();         int n = nums.length;         for(int i = 0; i &lt; n; i++) {             \/\/\u7ef4\u62a4\u5355\u8c03\u6027             while(!qmax.isEmpty() &amp;&amp; nums[i] &gt; nums[qmax.getLast()])                 qmax.removeLast();             \/\/\u6ed1\u52a8\u7a97\u53e3\u7684\u957f\u5ea6\u8d85\u8fc7\u4e86 k \u5220\u6389\u8d85\u8fc7\u7684\u90e8\u5206             if(!qmax.isEmpty() &amp;&amp; i - qmax.getFirst() &gt;= k)                 qmax.removeFirst();              qmax.addLast(i);              \/\/\u7edf\u8ba1\u7b54\u6848             if(i &gt;= k - 1) {                 ans.add(nums[qmax.getFirst()]);             }         }         return ans;     } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4279019\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zdliu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             var ary = [5, 1, 3, 5, 5, 6, 7, 8, 9, 12, 59, 24];<\/p>\n<p>function test(ary, num) {<br \/> for (let i = 0; i &lt; ary.length &#8211; num; i++) {<br \/> const list = [&#8230;ary].splice(i, num);<br \/> console.log(&#8216;list&#8217;, list, Math.max.apply(null, list));<br \/> }<br \/>}<\/p>\n<p>test(ary, 4);<\/p>\n<p>\u524d\u7aef\uff0c\uff0c\uff0c\u66f4\u7b80\u5355                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4279020\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Chinsung <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9 for \u91cc\u9762\u5957\u4e2a while\uff0c\u600e\u4e48\u770b\u4e5f\u4e0d\u662f O(N)\u7684\u65f6\u95f4\u554a\uff0c\u8fd9\u9898\u611f\u89c9\u771f\u6b63 O(N)\u7684\u65f6\u95f4\u5e94\u8be5\u8fd8\u662f\u5f97 DP                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4279021\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : HaibaraAi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Chinsung &#8211; -\u7136\u800c\u4e3b\u5e94\u8be5\u662f\u5bf9\u7684\uff0c\u522b\u770b\u5faa\u73af\u591a\u5c11\u4e2a\uff0c\u4e00\u4e2a list \u91cc\u80fd\u88ab while remove \u7684\u603b\u6570\u5c31\u90a3\u4e48\u591a\uff0c2*O(N)\u4e5f\u662f O(N)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u8f6f\u9762\u8bd5\u9898\uff0c\u7565\u590d\u6742 \u8cc7\u6df1\u5927\u4f6c : &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200419"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200419"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/200419\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200419"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}