Skip to content

Commit

Permalink
add: two pointers samples
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Feb 4, 2025
1 parent fc78ae5 commit eeff778
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 8 deletions.
97 changes: 97 additions & 0 deletions data_structures/src/KeyPatterns/SlidingWindow/Practice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package KeyPatterns.SlidingWindow;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;

/**
* Sliding Window Practice
*/
public class Practice {
public static void Examples() {
System.out.println("\n(Practice) Sliding Window ==> ");
maxSlidingWindowUsage();
hasPairWithSum();
removeDuplicates();
}

/**
* (Two Pointers)
*/
public static void removeDuplicates() {
System.out.println("\n(Two Pointers) Remove Duplicates");
int[] nums = { 1, 1, 2, 3, 3, 4, 5 };
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
if (nums[fast] != nums[slow]) {
slow++;
nums[slow] = nums[fast];
}
}
int ans = slow + 1;
System.out.println("ans ==> " + ans);
}

/**
* (Two Pointers) has Pair Sum
* Finding a Pair with a Given Sum (Sorted Array)
*/
public static void hasPairWithSum() {
System.out.println("\n(Two Pointers) has Pair with Sum ==> ");
int[] nums = { 1, 2, 3, 5, 8, 10 };
int target = 10;

class Helper {
boolean solution() {
int left = 0, right = nums.length - 1;

while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target)
return true;

if (sum < target)
left++;
else
right--;
}

return false;
}
}

Helper helper = new Helper();
System.out.println(helper.solution());
}

/**
* Given an array and an integer K, find the maximum for each and every
* contiguous subarray of size K.
*/
public static void maxSlidingWindowUsage() {
int[] arr = { 2, 3, 7, 9, 5, 1, 6, 4, 3 };
int k = 3;
List<Integer> max_sliding_result = maxSlidingWindow(arr, k);
System.out.println("max_sliding_result ==> " + max_sliding_result);
}

private static List<Integer> maxSlidingWindow(int arr[], int k) {
List<Integer> ans = new ArrayList<>();
TreeMap<Integer, Integer> treeMap = new TreeMap<>(Collections.reverseOrder());

for (int i = 0; i < k; i++) {
treeMap.put(arr[i], i);
}

ans.add(treeMap.firstKey());

for (int i = k; i < arr.length; i++) {
treeMap.put(arr[i], i);
treeMap.remove(arr[i - k]);
ans.add(treeMap.firstKey());
}

return ans;
}
}
17 changes: 9 additions & 8 deletions data_structures/src/KeyPatterns/SlidingWindow/SlidingWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ public class SlidingWindow {
*/
public static void Examples() {
System.out.println("\nSliding Window");
PrintSubArrays();
SubArrayExtractInJava();
PrintSubString();
ExtractSubString();
WindowSlideSample();
maxSumTwoNestedLoop();
maxSumOptmized();
VariableSizedSlideWindow.Examples();
// PrintSubArrays();
// SubArrayExtractInJava();
// PrintSubString();
// ExtractSubString();
// WindowSlideSample();
// maxSumTwoNestedLoop();
// maxSumOptmized();
// VariableSizedSlideWindow.Examples();
Practice.Examples();
}

public static void maxSumOptmized() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package KeyPatterns.SlidingWindow;

import java.util.HashSet;

/**
* A variable-sized sliding window dynamically expands and shrinks based on
* conditions instead of having a fixed length like in the standard sliding
Expand All @@ -14,6 +16,31 @@ public class VariableSizedSlideWindow {
public static void Examples() {
largestSubArraySum();
smallestSubArrayWithSum();
longestSubStringLength();
}

/**
* Longest SubString Length
* - Input: "abcabcbb"
* - Valid substrings: "abc", "bca", "cab", "abc", etc.
* - Output: 3 ("abc" or "bca" or "cab")
*/
public static void longestSubStringLength() {
String s = "abcabcbb";
System.out.println("\n(Variable Sized) Longest SubString Length ==> ");
int start = 0, maxLength = 0;
HashSet<Character> window = new HashSet<>();

for (int end = 0; end < s.length(); end++) {
while (window.contains(s.charAt(end))) {
window.remove(s.charAt(start));
start++;
}
window.add(s.charAt(end));
maxLength = Math.max(maxLength, end - start + 1);
}

System.out.println("maxLength ==> " + maxLength);
}

public static void smallestSubArrayWithSum() {
Expand Down

0 comments on commit eeff778

Please sign in to comment.