Skip to content

Commit aa1fbcc

Browse files
committed
add two pointers pattern
1 parent 3f52ae5 commit aa1fbcc

File tree

7 files changed

+424
-0
lines changed

7 files changed

+424
-0
lines changed

patterns/c#/TwoPointers.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
3+
public class TwoPointers {
4+
// Move Zeroes using Two Pointers
5+
public void MoveZeroesTwoPointers(int[] nums) {
6+
int left = 0; // Pointer for placing non-zero elements
7+
8+
// Iterate with right pointer
9+
for (int right = 0; right < nums.Length; right++) {
10+
if (nums[right] != 0) {
11+
// Swap elements if right pointer finds a non-zero
12+
(nums[left], nums[right]) = (nums[right], nums[left]);
13+
left++; // Move left pointer forward
14+
}
15+
}
16+
}
17+
18+
// Brute Force approach for Container with Most Water
19+
public int MaxAreaBruteForce(int[] height) {
20+
int n = height.Length;
21+
int maxArea = 0;
22+
23+
// Check all pairs (i, j)
24+
for (int i = 0; i < n; i++) {
25+
for (int j = i + 1; j < n; j++) {
26+
// Compute the minimum height and width
27+
int minHeight = Math.Min(height[i], height[j]);
28+
int width = j - i;
29+
int area = minHeight * width; // Compute water contained
30+
31+
maxArea = Math.Max(maxArea, area); // Update max water
32+
}
33+
}
34+
return maxArea;
35+
}
36+
37+
// Two Pointers approach for Container with Most Water
38+
public int MaxAreaTwoPointers(int[] height) {
39+
int left = 0, right = height.Length - 1;
40+
int maxArea = 0;
41+
42+
// Move pointers toward each other
43+
while (left < right) {
44+
int width = right - left; // Distance between lines
45+
int minHeight = Math.Min(height[left], height[right]); // Compute height
46+
int area = minHeight * width; // Compute water contained
47+
48+
maxArea = Math.Max(maxArea, area); // Update max water
49+
50+
// Move the pointer pointing to the shorter height
51+
if (height[left] < height[right]) {
52+
left++; // Move left pointer forward
53+
} else {
54+
right--; // Move right pointer backward
55+
}
56+
}
57+
return maxArea;
58+
}
59+
}

patterns/c++/TwoPointers.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
class TwoPointers {
8+
public:
9+
// Move Zeroes using Two Pointers
10+
void moveZeroesTwoPointers(vector<int>& nums) {
11+
int left = 0; // Pointer for placing non-zero elements
12+
13+
// Iterate with right pointer
14+
for (int right = 0; right < nums.size(); right++) {
15+
if (nums[right] != 0) {
16+
// Swap elements if right pointer finds a non-zero
17+
swap(nums[left], nums[right]);
18+
left++; // Move left pointer forward
19+
}
20+
}
21+
}
22+
23+
// Brute Force approach for Container with Most Water
24+
int maxAreaBruteForce(vector<int>& height) {
25+
int n = height.size();
26+
int maxArea = 0;
27+
28+
// Check all pairs (i, j)
29+
for (int i = 0; i < n; i++) {
30+
for (int j = i + 1; j < n; j++) {
31+
// Compute the minimum height and width
32+
int minHeight = min(height[i], height[j]);
33+
int width = j - i;
34+
int area = minHeight * width; // Compute water contained
35+
36+
maxArea = max(maxArea, area); // Update max water
37+
}
38+
}
39+
return maxArea;
40+
}
41+
42+
// Two Pointers approach for Container with Most Water
43+
int maxAreaTwoPointers(vector<int>& height) {
44+
int left = 0, right = height.size() - 1;
45+
int maxArea = 0;
46+
47+
// Move pointers toward each other
48+
while (left < right) {
49+
int width = right - left; // Distance between lines
50+
int minHeight = min(height[left], height[right]); // Compute height
51+
int area = minHeight * width; // Compute water contained
52+
53+
maxArea = max(maxArea, area); // Update max water
54+
55+
// Move the pointer pointing to the shorter height
56+
if (height[left] < height[right]) {
57+
left++; // Move left pointer forward
58+
} else {
59+
right--; // Move right pointer backward
60+
}
61+
}
62+
return maxArea;
63+
}
64+
};

patterns/go/two_pointers.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
// Move Zeroes using Two Pointers
4+
func moveZeroesTwoPointers(nums []int) {
5+
left := 0 // Pointer for placing non-zero elements
6+
7+
// Iterate with right pointer
8+
for right := 0; right < len(nums); right++ {
9+
if nums[right] != 0 {
10+
// Swap elements if right pointer finds a non-zero
11+
nums[left], nums[right] = nums[right], nums[left]
12+
left++ // Move left pointer forward
13+
}
14+
}
15+
}
16+
17+
// Brute Force approach for Container with Most Water
18+
func maxAreaBruteForce(height []int) int {
19+
n := len(height)
20+
maxArea := 0
21+
22+
// Check all pairs (i, j)
23+
for i := 0; i < n; i++ {
24+
for j := i + 1; j < n; j++ {
25+
// Compute the minimum height and width
26+
minHeight := min(height[i], height[j])
27+
width := j - i
28+
area := minHeight * width // Compute water contained
29+
30+
if area > maxArea {
31+
maxArea = area // Update max water
32+
}
33+
}
34+
}
35+
return maxArea
36+
}
37+
38+
// Two Pointers approach for Container with Most Water
39+
func maxAreaTwoPointers(height []int) int {
40+
left, right := 0, len(height)-1
41+
maxArea := 0
42+
43+
// Move pointers toward each other
44+
for left < right {
45+
width := right - left // Distance between lines
46+
minHeight := min(height[left], height[right]) // Compute height
47+
area := minHeight * width // Compute water contained
48+
49+
if area > maxArea {
50+
maxArea = area // Update max water
51+
}
52+
53+
// Move the pointer pointing to the shorter height
54+
if height[left] < height[right] {
55+
left++ // Move left pointer forward
56+
} else {
57+
right-- // Move right pointer backward
58+
}
59+
}
60+
return maxArea
61+
}
62+
63+
// Helper function to return the minimum of two integers
64+
func min(a, b int) int {
65+
if a < b {
66+
return a
67+
}
68+
return b
69+
}

patterns/java/TwoPointers.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package patterns.java;
2+
3+
public class TwoPointers {
4+
5+
public void moveZeroesTwoPointers(int[] nums) {
6+
int left = 0; // Pointer for placing non-zero elements
7+
8+
// Iterate with right pointer
9+
for (int right = 0; right < nums.length; right++) {
10+
if (nums[right] != 0) {
11+
// Swap elements if right pointer finds a non-zero
12+
int temp = nums[left];
13+
nums[left] = nums[right];
14+
nums[right] = temp;
15+
left++; // Move left pointer forward
16+
}
17+
}
18+
}
19+
20+
public int maxAreaBruteForce(int[] height) {
21+
int n = height.length;
22+
int maxArea = 0;
23+
24+
// Check all pairs (i, j)
25+
for (int i = 0; i < n; i++) {
26+
for (int j = i + 1; j < n; j++) {
27+
// Height of the container
28+
int minHeight = Math.min(height[i], height[j]);
29+
int width = j - i; // Distance between lines
30+
int area = minHeight * width; // Compute water contained
31+
32+
maxArea = Math.max(maxArea, area); // Update max water
33+
}
34+
}
35+
return maxArea;
36+
}
37+
38+
public int maxAreaTwoPointers(int[] height) {
39+
int left = 0, right = height.length - 1;
40+
int maxArea = 0;
41+
42+
// Move pointers toward each other
43+
while (left <= right) {
44+
int width = right - left; // Distance between lines
45+
int minHeight = Math.min(height[left], height[right]);
46+
int area = minHeight * width; // Compute water contained
47+
48+
maxArea = Math.max(maxArea, area); // Update max water
49+
50+
// Move the pointer pointing to the shorter height
51+
if (height[left] < height[right]) {
52+
left++; // Move left pointer forward
53+
} else {
54+
right--; // Move right pointer backward
55+
}
56+
}
57+
return maxArea;
58+
}
59+
}

patterns/javascript/twoPointers.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class TwoPointers {
2+
/**
3+
* Move Zeroes using Two Pointers
4+
* @param {number[]} nums - Input array
5+
*/
6+
moveZeroesTwoPointers(nums) {
7+
let left = 0; // Pointer for placing non-zero elements
8+
9+
// Iterate with right pointer
10+
for (let right = 0; right < nums.length; right++) {
11+
if (nums[right] !== 0) {
12+
// Swap elements if right pointer finds a non-zero
13+
[nums[left], nums[right]] = [nums[right], nums[left]];
14+
left++; // Move left pointer forward
15+
}
16+
}
17+
}
18+
19+
/**
20+
* Brute Force approach for Container with Most Water
21+
* @param {number[]} height - Array of heights
22+
* @return {number} - Maximum water that can be contained
23+
*/
24+
maxAreaBruteForce(height) {
25+
let maxArea = 0;
26+
let n = height.length;
27+
28+
// Check all pairs (i, j)
29+
for (let i = 0; i < n; i++) {
30+
for (let j = i + 1; j < n; j++) {
31+
// Compute the minimum height and width
32+
let minHeight = Math.min(height[i], height[j]);
33+
let width = j - i;
34+
let area = minHeight * width; // Compute water contained
35+
36+
maxArea = Math.max(maxArea, area); // Update max water
37+
}
38+
}
39+
return maxArea;
40+
}
41+
42+
/**
43+
* Two Pointers approach for Container with Most Water
44+
* @param {number[]} height - Array of heights
45+
* @return {number} - Maximum water that can be contained
46+
*/
47+
maxAreaTwoPointers(height) {
48+
let left = 0, right = height.length - 1;
49+
let maxArea = 0;
50+
51+
// Move pointers toward each other
52+
while (left < right) {
53+
let width = right - left; // Distance between lines
54+
let minHeight = Math.min(height[left], height[right]); // Compute height
55+
let area = minHeight * width; // Compute water contained
56+
57+
maxArea = Math.max(maxArea, area); // Update max water
58+
59+
// Move the pointer pointing to the shorter height
60+
if (height[left] < height[right]) {
61+
left++; // Move left pointer forward
62+
} else {
63+
right--; // Move right pointer backward
64+
}
65+
}
66+
return maxArea;
67+
}
68+
}

patterns/python/two_pointers.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class TwoPointers:
2+
# Move Zeroes using Two Pointers
3+
def move_zeroes_two_pointers(self, nums):
4+
left = 0 # Pointer for placing non-zero elements
5+
6+
# Iterate with right pointer
7+
for right in range(len(nums)):
8+
if nums[right] != 0:
9+
# Swap elements if right pointer finds a non-zero
10+
nums[left], nums[right] = nums[right], nums[left]
11+
left += 1 # Move left pointer forward
12+
13+
# Brute Force approach for Container with Most Water
14+
def max_area_brute_force(self, height):
15+
n = len(height)
16+
max_area = 0
17+
18+
# Check all pairs (i, j)
19+
for i in range(n):
20+
for j in range(i + 1, n):
21+
# Compute the minimum height and width
22+
min_height = min(height[i], height[j])
23+
width = j - i
24+
area = min_height * width # Compute water contained
25+
26+
max_area = max(max_area, area) # Update max water
27+
return max_area
28+
29+
# Two Pointers approach for Container with Most Water
30+
def max_area_two_pointers(self, height):
31+
left, right = 0, len(height) - 1
32+
max_area = 0
33+
34+
# Move pointers toward each other
35+
while left < right:
36+
width = right - left # Distance between lines
37+
min_height = min(height[left], height[right]) # Compute height
38+
area = min_height * width # Compute water contained
39+
40+
max_area = max(max_area, area) # Update max water
41+
42+
# Move the pointer pointing to the shorter height
43+
if height[left] < height[right]:
44+
left += 1 # Move left pointer forward
45+
else:
46+
right -= 1 # Move right pointer backward
47+
48+
return max_area

0 commit comments

Comments
 (0)