Back to Leetcode Go

2.17 ✅ Sliding Window

website/content.en/ChapterTwo/Sliding_Window.md

1.7.976.2 KB
Original Source

Sliding Window

  • The classic way to write a two-pointer sliding window. The right pointer keeps moving to the right until it can no longer move right (the specific condition depends on the problem). After the right pointer reaches the far right, start moving the left pointer to release the left boundary of the window. Problem 3, Problem 76, Problem 209, Problem 424, Problem 438, Problem 567, Problem 713, Problem 763, Problem 845, Problem 881, Problem 904, Problem 978, Problem 992, Problem 1004, Problem 1040, Problem 1052.
c
	left, right := 0, -1

	for left < len(s) {
		if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
			freq[s[right+1]-'a']++
			right++
		} else {
			freq[s[left]-'a']--
			left++
		}
		result = max(result, right-left+1)
	}
  • Classic sliding window problems. Problem 239, Problem 480.
No.TitleSolutionDifficultyTimeComplexitySpaceComplexityFavoriteAcceptance
0003Longest Substring Without Repeating Characters[Go]({{< relref "/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters.md" >}})MediumO(n)O(1)❤️33.8%
0030Substring with Concatenation of All Words[Go]({{< relref "/ChapterFour/0001~0099/0030.Substring-with-Concatenation-of-All-Words.md" >}})Hard31.2%
0076Minimum Window Substring[Go]({{< relref "/ChapterFour/0001~0099/0076.Minimum-Window-Substring.md" >}})HardO(n)O(n)❤️40.9%
0187Repeated DNA Sequences[Go]({{< relref "/ChapterFour/0100~0199/0187.Repeated-DNA-Sequences.md" >}})Medium47.0%
0209Minimum Size Subarray Sum[Go]({{< relref "/ChapterFour/0200~0299/0209.Minimum-Size-Subarray-Sum.md" >}})Medium45.0%
0219Contains Duplicate II[Go]({{< relref "/ChapterFour/0200~0299/0219.Contains-Duplicate-II.md" >}})Easy42.6%
0220Contains Duplicate III[Go]({{< relref "/ChapterFour/0200~0299/0220.Contains-Duplicate-III.md" >}})Hard22.1%
0239Sliding Window Maximum[Go]({{< relref "/ChapterFour/0200~0299/0239.Sliding-Window-Maximum.md" >}})HardO(n * k)O(n)❤️46.3%
0395Longest Substring with At Least K Repeating Characters[Go]({{< relref "/ChapterFour/0300~0399/0395.Longest-Substring-with-At-Least-K-Repeating-Characters.md" >}})Medium44.8%
0424Longest Repeating Character Replacement[Go]({{< relref "/ChapterFour/0400~0499/0424.Longest-Repeating-Character-Replacement.md" >}})MediumO(n)O(1)52.0%
0438Find All Anagrams in a String[Go]({{< relref "/ChapterFour/0400~0499/0438.Find-All-Anagrams-in-a-String.md" >}})Medium50.2%
0480Sliding Window Median[Go]({{< relref "/ChapterFour/0400~0499/0480.Sliding-Window-Median.md" >}})HardO(n * log k)O(k)❤️41.1%
0567Permutation in String[Go]({{< relref "/ChapterFour/0500~0599/0567.Permutation-in-String.md" >}})MediumO(n)O(1)❤️44.3%
0632Smallest Range Covering Elements from K Lists[Go]({{< relref "/ChapterFour/0600~0699/0632.Smallest-Range-Covering-Elements-from-K-Lists.md" >}})Hard61.0%
0643Maximum Average Subarray I[Go]({{< relref "/ChapterFour/0600~0699/0643.Maximum-Average-Subarray-I.md" >}})Easy43.7%
0658Find K Closest Elements[Go]({{< relref "/ChapterFour/0600~0699/0658.Find-K-Closest-Elements.md" >}})Medium46.8%
0713Subarray Product Less Than K[Go]({{< relref "/ChapterFour/0700~0799/0713.Subarray-Product-Less-Than-K.md" >}})Medium45.8%
0718Maximum Length of Repeated Subarray[Go]({{< relref "/ChapterFour/0700~0799/0718.Maximum-Length-of-Repeated-Subarray.md" >}})Medium51.3%
0862Shortest Subarray with Sum at Least K[Go]({{< relref "/ChapterFour/0800~0899/0862.Shortest-Subarray-with-Sum-at-Least-K.md" >}})Hard26.0%
0904Fruit Into Baskets[Go]({{< relref "/ChapterFour/0900~0999/0904.Fruit-Into-Baskets.md" >}})Medium43.7%
0930Binary Subarrays With Sum[Go]({{< relref "/ChapterFour/0900~0999/0930.Binary-Subarrays-With-Sum.md" >}})Medium52.2%
0978Longest Turbulent Subarray[Go]({{< relref "/ChapterFour/0900~0999/0978.Longest-Turbulent-Subarray.md" >}})MediumO(n)O(1)❤️47.2%
0992Subarrays with K Different Integers[Go]({{< relref "/ChapterFour/0900~0999/0992.Subarrays-with-K-Different-Integers.md" >}})HardO(n)O(n)❤️54.6%
0995Minimum Number of K Consecutive Bit Flips[Go]({{< relref "/ChapterFour/0900~0999/0995.Minimum-Number-of-K-Consecutive-Bit-Flips.md" >}})HardO(n)O(1)❤️51.2%
1004Max Consecutive Ones III[Go]({{< relref "/ChapterFour/1000~1099/1004.Max-Consecutive-Ones-III.md" >}})MediumO(n)O(1)63.2%
1052Grumpy Bookstore Owner[Go]({{< relref "/ChapterFour/1000~1099/1052.Grumpy-Bookstore-Owner.md" >}})MediumO(n log n)O(1)57.1%
1208Get Equal Substrings Within Budget[Go]({{< relref "/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget.md" >}})Medium48.6%
1234Replace the Substring for Balanced String[Go]({{< relref "/ChapterFour/1200~1299/1234.Replace-the-Substring-for-Balanced-String.md" >}})Medium37.2%
1423Maximum Points You Can Obtain from Cards[Go]({{< relref "/ChapterFour/1400~1499/1423.Maximum-Points-You-Can-Obtain-from-Cards.md" >}})Medium52.2%
1438Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit[Go]({{< relref "/ChapterFour/1400~1499/1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit.md" >}})Medium48.3%
1658Minimum Operations to Reduce X to Zero[Go]({{< relref "/ChapterFour/1600~1699/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})Medium37.6%
1695Maximum Erasure Value[Go]({{< relref "/ChapterFour/1600~1699/1695.Maximum-Erasure-Value.md" >}})Medium57.6%
1696Jump Game VI[Go]({{< relref "/ChapterFour/1600~1699/1696.Jump-Game-VI.md" >}})Medium46.1%
1763Longest Nice Substring[Go]({{< relref "/ChapterFour/1700~1799/1763.Longest-Nice-Substring.md" >}})Easy61.5%
1984Minimum Difference Between Highest and Lowest of K Scores[Go]({{< relref "/ChapterFour/1900~1999/1984.Minimum-Difference-Between-Highest-and-Lowest-of-K-Scores.md" >}})Easy54.5%
------------------------------------------------------------------------------------------------------------------------------------------------