Skip to content

Commit dd01e8e

Browse files
committed
feat:add two pointer algorithm
1 parent c5c6d0c commit dd01e8e

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

test/two_pointer.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <cassert>
2+
#include <iostream>
3+
#include <vector>
4+
5+
/**
6+
* @brief Calculates the maximum water that can be trapped between vertical
7+
* lines on a 2D plane.
8+
*
9+
* Given an array of non-negative integers representing vertical lines on a 2D
10+
* plane, where the width of each line is 1, the function finds two lines that,
11+
* together with the x-axis, form a container that can hold the most water.
12+
*
13+
* The problem can be efficiently solved using a two-pointer approach. We
14+
* initialize two pointers, 'left' and 'right', at the beginning and end of the
15+
* array, respectively. The width of the container is determined by the
16+
* difference between the 'right' and 'left' pointers. To maximize the water
17+
* trapped, we choose the minimum height between the lines at the 'left' and
18+
* 'right' positions. We then calculate the water trapped by multiplying the
19+
* width and height. We move the pointers towards each other, adjusting them
20+
* based on the shorter line, as moving the pointer with the smaller height has
21+
* the potential to increase the water volume.
22+
*
23+
* @param height Vector representing the heights of vertical lines.
24+
* @return int Maximum water that can be trapped.
25+
*/
26+
27+
int maxArea(const std::vector<int>& height) {
28+
int maxWater = 0;
29+
int left = 0;
30+
int right = height.size() - 1;
31+
32+
while (left < right) {
33+
int width = right - left;
34+
int h = std::min(height[left], height[right]);
35+
int currentWater = width * h;
36+
maxWater = std::max(maxWater, currentWater);
37+
38+
if (height[left] < height[right]) {
39+
left++;
40+
} else {
41+
right--;
42+
}
43+
}
44+
45+
return maxWater;
46+
}
47+
/**
48+
* @brief Tests the maxArea function with various test cases.
49+
*
50+
* The function iterates through each test case, calculates the result using the
51+
* maxArea function, checks if the result matches the expected result, and
52+
* prints a success message for each test case.
53+
*/
54+
55+
void testContainerWithMostWater() {
56+
// Test cases with expected maximum water values
57+
std::vector<std::pair<std::vector<int>, int>> testCases = {
58+
{{1, 8, 6, 2, 5, 4, 8, 3, 7}, 49},
59+
{{1, 1}, 1},
60+
{{4, 3, 2, 1, 4}, 16},
61+
{{1, 2, 1}, 2},
62+
};
63+
for (const auto& testCase : testCases) {
64+
const std::vector<int>& input = testCase.first;
65+
int expected = testCase.second;
66+
67+
// Calculate the result using the maxArea function
68+
int result = maxArea(input);
69+
assert(result == expected);
70+
std::cout << "Test passed!" << std::endl;
71+
}
72+
}
73+
74+
int main() {
75+
testContainerWithMostWater();
76+
return 0;
77+
}

0 commit comments

Comments
 (0)