Skip to content

Commit 3bb0171

Browse files
committed
add sliding window pattern
1 parent 021e97d commit 3bb0171

File tree

7 files changed

+484
-0
lines changed

7 files changed

+484
-0
lines changed

patterns/c#/SlidingWindow.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class SlidingWindow {
5+
public double FindMaxAverageBruteForce(int[] nums, int k) {
6+
int n = nums.Length;
7+
double maxAvg = double.MinValue;
8+
9+
for (int i = 0; i <= n - k; i++) {
10+
int sum = 0;
11+
for (int j = i; j < i + k; j++) {
12+
sum += nums[j];
13+
}
14+
maxAvg = Math.Max(maxAvg, (double)sum / k);
15+
}
16+
return maxAvg;
17+
}
18+
19+
public double FindMaxAverageSlidingWindow(int[] nums, int k) {
20+
int n = nums.Length;
21+
int sum = 0;
22+
23+
for (int i = 0; i < k; i++) {
24+
sum += nums[i];
25+
}
26+
27+
int maxSum = sum;
28+
29+
for (int i = k; i < n; i++) {
30+
sum += nums[i];
31+
sum -= nums[i - k];
32+
maxSum = Math.Max(maxSum, sum);
33+
}
34+
35+
return (double)maxSum / k;
36+
}
37+
38+
public int LengthOfLongestSubstringSlidingWindow(string s) {
39+
HashSet<char> seen = new HashSet<char>();
40+
int maxLength = 0, left = 0;
41+
42+
for (int right = 0; right < s.Length; right++) {
43+
while (seen.Contains(s[right])) {
44+
seen.Remove(s[left]);
45+
left++;
46+
}
47+
seen.Add(s[right]);
48+
maxLength = Math.Max(maxLength, right - left + 1);
49+
}
50+
return maxLength;
51+
}
52+
53+
public int LengthOfLongestSubstringSlidingWindowFrequencyArray(string s) {
54+
int[] freq = new int[128];
55+
int maxLength = 0, left = 0;
56+
57+
for (int right = 0; right < s.Length; right++) {
58+
freq[s[right]]++;
59+
60+
while (freq[s[right]] > 1) {
61+
freq[s[left]]--;
62+
left++;
63+
}
64+
65+
maxLength = Math.Max(maxLength, right - left + 1);
66+
}
67+
return maxLength;
68+
}
69+
}

patterns/c++/SlidingWindow.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <unordered_set>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
class SlidingWindow {
9+
public:
10+
double findMaxAverageBruteForce(vector<int>& nums, int k) {
11+
int n = nums.size();
12+
double maxAvg = INT_MIN;
13+
14+
for (int i = 0; i <= n - k; i++) {
15+
int sum = 0;
16+
for (int j = i; j < i + k; j++) {
17+
sum += nums[j];
18+
}
19+
maxAvg = max(maxAvg, (double)sum / k);
20+
}
21+
return maxAvg;
22+
}
23+
24+
double findMaxAverageSlidingWindow(vector<int>& nums, int k) {
25+
int n = nums.size();
26+
int sum = 0;
27+
28+
for (int i = 0; i < k; i++) {
29+
sum += nums[i];
30+
}
31+
32+
int maxSum = sum;
33+
34+
for (int i = k; i < n; i++) {
35+
sum += nums[i];
36+
sum -= nums[i - k];
37+
maxSum = max(maxSum, sum);
38+
}
39+
40+
return (double)maxSum / k;
41+
}
42+
43+
int lengthOfLongestSubstringSlidingWindow(string s) {
44+
unordered_set<char> seen;
45+
int maxLength = 0, left = 0;
46+
47+
for (int right = 0; right < s.size(); right++) {
48+
while (seen.count(s[right])) {
49+
seen.erase(s[left]);
50+
left++;
51+
}
52+
seen.insert(s[right]);
53+
maxLength = max(maxLength, right - left + 1);
54+
}
55+
return maxLength;
56+
}
57+
58+
int lengthOfLongestSubstringSlidingWindowFrequencyArray(string s) {
59+
vector<int> freq(128, 0);
60+
int maxLength = 0, left = 0;
61+
62+
for (int right = 0; right < s.size(); right++) {
63+
freq[s[right]]++;
64+
65+
while (freq[s[right]] > 1) {
66+
freq[s[left]]--;
67+
left++;
68+
}
69+
70+
maxLength = max(maxLength, right - left + 1);
71+
}
72+
return maxLength;
73+
}
74+
};

patterns/go/sliding_window.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"math"
5+
)
6+
7+
// Brute Force Approach - O(n * k)
8+
func findMaxAverageBruteForce(nums []int, k int) float64 {
9+
n := len(nums)
10+
maxAvg := math.Inf(-1)
11+
12+
for i := 0; i <= n-k; i++ {
13+
sum := 0
14+
for j := i; j < i+k; j++ {
15+
sum += nums[j]
16+
}
17+
maxAvg = math.Max(maxAvg, float64(sum)/float64(k))
18+
}
19+
return maxAvg
20+
}
21+
22+
// Sliding Window Approach - O(n)
23+
func findMaxAverageSlidingWindow(nums []int, k int) float64 {
24+
sum := 0
25+
for i := 0; i < k; i++ {
26+
sum += nums[i]
27+
}
28+
29+
maxSum := sum
30+
31+
for i := k; i < len(nums); i++ {
32+
sum += nums[i] - nums[i-k]
33+
if sum > maxSum {
34+
maxSum = sum
35+
}
36+
}
37+
38+
return float64(maxSum) / float64(k)
39+
}
40+
41+
// Sliding Window for Longest Substring Without Repeating Characters
42+
func lengthOfLongestSubstringSlidingWindow(s string) int {
43+
seen := make(map[byte]bool)
44+
maxLength, left := 0, 0
45+
46+
for right := 0; right < len(s); right++ {
47+
for seen[s[right]] {
48+
delete(seen, s[left])
49+
left++
50+
}
51+
seen[s[right]] = true
52+
maxLength = max(maxLength, right-left+1)
53+
}
54+
return maxLength
55+
}
56+
57+
// Sliding Window using Frequency Array
58+
func lengthOfLongestSubstringSlidingWindowFrequencyArray(s string) int {
59+
freq := make([]int, 128)
60+
maxLength, left := 0, 0
61+
62+
for right := 0; right < len(s); right++ {
63+
freq[s[right]]++
64+
65+
for freq[s[right]] > 1 {
66+
freq[s[left]]--
67+
left++
68+
}
69+
70+
maxLength = max(maxLength, right-left+1)
71+
}
72+
return maxLength
73+
}
74+
75+
// Helper function to get max of two numbers
76+
func max(a, b int) int {
77+
if a > b {
78+
return a
79+
}
80+
return b
81+
}

patterns/java/SlidingWindow.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package patterns.java;
2+
3+
import java.util.HashSet;
4+
5+
public class SlidingWindow {
6+
public double findMaxAverageBruteForce(int[] nums, int k) {
7+
int n = nums.length;
8+
double maxAvg = Integer.MIN_VALUE;
9+
10+
// Iterate through all possible subarrays of length k
11+
for (int i = 0; i <= n - k; i++) {
12+
int sum = 0;
13+
14+
// Calculate sum of subarray starting at index i
15+
for (int j = i; j < i + k; j++) {
16+
sum += nums[j];
17+
}
18+
19+
// Compute average and update maxAvg
20+
maxAvg = Math.max(maxAvg, (double) sum / k);
21+
}
22+
return maxAvg;
23+
}
24+
25+
public double findMaxAverageSlidingWindow(int[] nums, int k) {
26+
int n = nums.length;
27+
28+
// Compute the sum of the first 'k' elements
29+
int sum = 0;
30+
for (int i = 0; i < k; i++) {
31+
sum += nums[i];
32+
}
33+
34+
// Initialize maxSum as the sum of the first window
35+
int maxSum = sum;
36+
37+
// Slide the window across the array
38+
for (int i = k; i < n; i++) {
39+
sum += nums[i]; // Add new element entering window
40+
sum -= nums[i - k]; // Remove element leaving window
41+
maxSum = Math.max(maxSum, sum); // Update maxSum
42+
}
43+
44+
// Return maximum average
45+
return (double) maxSum / k;
46+
}
47+
48+
public int lengthOfLongestSubstringSlidingWindow(String s) {
49+
int n = s.length();
50+
HashSet<Character> seen = new HashSet<>(); // Store characters in the current window
51+
int maxLength = 0;
52+
int left = 0;
53+
54+
// Expand window by moving 'right'
55+
for (int right = 0; right < n; right++) {
56+
// If a duplicate is found, shrink the window from the left
57+
while (seen.contains(s.charAt(right))) {
58+
seen.remove(s.charAt(left));
59+
left++;
60+
}
61+
// Add current character to window and update max length
62+
seen.add(s.charAt(right));
63+
maxLength = Math.max(maxLength, right - left + 1);
64+
}
65+
return maxLength;
66+
}
67+
68+
public int lengthOfLongestSubstringSlidingWindowFrequencyArray(String s) {
69+
int n = s.length();
70+
int[] freq = new int[128]; // ASCII character frequency array
71+
int maxLength = 0;
72+
int left = 0;
73+
74+
// Expand window by moving 'right'
75+
for (int right = 0; right < n; right++) {
76+
char currentChar = s.charAt(right);
77+
freq[currentChar]++; // Increase frequency of the current character
78+
79+
// If there is a duplicate, shrink the window from the left
80+
while (freq[currentChar] > 1) {
81+
freq[s.charAt(left)]--; // Remove character at left pointer
82+
left++; // Shrink window
83+
}
84+
85+
// Update maximum window size
86+
maxLength = Math.max(maxLength, right - left + 1);
87+
}
88+
return maxLength;
89+
}
90+
}

patterns/javascript/slidingWindow.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class SlidingWindow {
2+
// Brute Force Approach - O(n * k)
3+
findMaxAverageBruteForce(nums, k) {
4+
let maxAvg = -Infinity;
5+
6+
for (let i = 0; i <= nums.length - k; i++) {
7+
let sum = 0;
8+
for (let j = i; j < i + k; j++) {
9+
sum += nums[j];
10+
}
11+
maxAvg = Math.max(maxAvg, sum / k);
12+
}
13+
return maxAvg;
14+
}
15+
16+
// Sliding Window Approach - O(n)
17+
findMaxAverageSlidingWindow(nums, k) {
18+
let sum = nums.slice(0, k).reduce((a, b) => a + b, 0);
19+
let maxSum = sum;
20+
21+
for (let i = k; i < nums.length; i++) {
22+
sum += nums[i] - nums[i - k];
23+
maxSum = Math.max(maxSum, sum);
24+
}
25+
26+
return maxSum / k;
27+
}
28+
29+
// Sliding Window for Longest Substring Without Repeating Characters
30+
lengthOfLongestSubstringSlidingWindow(s) {
31+
let seen = new Set();
32+
let maxLength = 0, left = 0;
33+
34+
for (let right = 0; right < s.length; right++) {
35+
while (seen.has(s[right])) {
36+
seen.delete(s[left]);
37+
left++;
38+
}
39+
seen.add(s[right]);
40+
maxLength = Math.max(maxLength, right - left + 1);
41+
}
42+
return maxLength;
43+
}
44+
45+
// Sliding Window using Frequency Array
46+
lengthOfLongestSubstringSlidingWindowFrequencyArray(s) {
47+
let freq = new Array(128).fill(0);
48+
let maxLength = 0, left = 0;
49+
50+
for (let right = 0; right < s.length; right++) {
51+
freq[s.charCodeAt(right)]++;
52+
53+
while (freq[s.charCodeAt(right)] > 1) {
54+
freq[s.charCodeAt(left)]--;
55+
left++;
56+
}
57+
58+
maxLength = Math.max(maxLength, right - left + 1);
59+
}
60+
return maxLength;
61+
}
62+
}

0 commit comments

Comments
 (0)