{"id":1437,"date":"2020-07-30T16:29:00","date_gmt":"2020-07-30T23:29:00","guid":{"rendered":"http:\/\/209.126.2.187\/?p=1437"},"modified":"2020-08-02T17:15:59","modified_gmt":"2020-08-03T00:15:59","slug":"sliding-window-concepts","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2020\/07\/30\/sliding-window-concepts\/","title":{"rendered":"Sliding Window Concepts"},"content":{"rendered":"\n<p>The <strong>Sliding Window<\/strong> technique is usually used to find the <strong>shortest<\/strong> or <strong>longest<\/strong> <strong>qualified<\/strong> subarray. For example, find the shortest subarray whose sum is no less than K. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A Naive Idea<\/h3>\n\n\n\n<p>The problem is straightforward if, for every right index, we could find the rightmost left index so that the window has <code>sum >= K<\/code>. This can be done by another loop. The overall complexity will be <code>O(n^2)<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sliding Window<\/h3>\n\n\n\n<p>We could use the Sliding Window technique to improve the time complexity to <code>O(n)<\/code>, if the problem has the following monotonic property:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>We keep a window <code>(left, right]<\/code>;<\/li><li>As the index <code>right<\/code> increases, in the optimal solution, the <code>left<\/code> index won&#8217;t decrease. <\/li><\/ol>\n\n\n\n<p>Given the above property, it is not hard to see that the <code>left<\/code> index and the <code>right<\/code> index will only traverse the whole array once, which is of complexity <code>O(n)<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Shortest Subarray With Sum At Least K<\/h4>\n\n\n\n<p>If the window<code>(left, right]<\/code> qualifies, as <code>right<\/code> increases, <code>left<\/code> won&#8217;t decrease, since <code>(x, right+1], x&lt;left<\/code> must be longer than <code>(left, right]<\/code>. This property holds no matter numbers in the array are positive or negative.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Longest Subarray With At Most K 0s<\/h4>\n\n\n\n<p>If the window <code>(left, right]<\/code> qualifies, as <code>right<\/code> increases, <code>left<\/code> won&#8217;t decrease, since <code>(left, right]<\/code> is already the longest qualified subarray for all <code>(x, right]<\/code>, introducing <code>right+1<\/code> won&#8217;t decrease the number of 0s, so the index <code>left<\/code> can&#8217;t move left. <\/p>\n\n\n\n<p>Think in another way: if <code>(left, right]<\/code> contains more than K 0s, <code>(x, y] x&lt;left, y>right <\/code>can&#8217;t be qualified. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Longest Substring With At Most K Unique Characters<\/h4>\n\n\n\n<p>If the window <code>(left, right]<\/code> qualifies, as <code>right<\/code> increases, <code>left<\/code> won&#8217;t decrease, since <code>(left, right]<\/code> is already the longest qualified subarray for all <code>(x, right]<\/code>, introducing <code>right+1<\/code> won&#8217;t decrease the number of distinct characters, so the index <code>left<\/code> can&#8217;t move left.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Longest Substring With No Duplicate Characters<\/h4>\n\n\n\n<p>If the window <code>(left, right]<\/code> qualifies, as <code>right<\/code> increases, <code>left<\/code> won&#8217;t decrease, since <code>(left, right]<\/code> is already the longest qualified subarray for all <code>(x, right]<\/code>, introducing <code>right+1<\/code> won&#8217;t decrease the number of distinct characters, so the index <code>left<\/code> can&#8217;t move left.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Longest Substring With At Least K Same Characters <\/h4>\n\n\n\n<p>This problem is tricky. At first glance, the monotonic property doesn&#8217;t hold: If the window <code>(left, right]<\/code> qualifies, as <code>right<\/code> increases, <code>left<\/code> might decrease, since <code>right+1<\/code> introduces a new char, which might make some unqualified windows <code>(x, right], x&lt;left<\/code> qualified.<\/p>\n\n\n\n<p>However, if we introduce another constraint: find the longest substring with at least K same characters and L unique characters, the monotonic property holds:<\/p>\n\n\n\n<p>If the window <code>(left, right]<\/code> qualifies, as <code>right<\/code> increases, <code>left<\/code> won&#8217;t decrease, since <code>(left, right]<\/code> is already the longest qualified subarray for all <code>(x, right]<\/code>, introducing <code>right+1<\/code> won&#8217;t decrease the number of distinct characters, so the index <code>left<\/code> can&#8217;t move left.<\/p>\n\n\n\n<p>And we traverse L from <code>1<\/code> to <code>26<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Sliding Window technique is usually used to find the shortest or longest qualified subarray. For example, find the shortest subarray whose sum is no less than K. A Naive Idea The problem is straightforward if, for every right index, we could find the rightmost left index so that the window has sum >= K&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,62],"tags":[],"class_list":["post-1437","post","type-post","status-publish","format-standard","hentry","category-alg","category-sliding-window"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1437"}],"version-history":[{"count":1,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":1439,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1437\/revisions\/1439"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}