Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added "Two Pointers" approach visualization #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Uncategorized/Two Pointers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Two Pointers

Two pointers algorithm is a very useful technique in solving many problems, especially those that require scanning through arrays or lists.

One practical example of using the two pointers algorithm is in finding pairs of numbers in an array that add up to a given target value.

But, I prefer to provide example from the [leetcode](https://leetcode.com/problems/container-with-most-water/).

![question_11.jpg](img%2Fquestion_11.jpg)

```
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
```

## Complexity
* **Time**: O(n)
* **Space**: O(1)

## References:

* [DSA: Two-pointers algorithm. Review with the step-by-step guide](https://medium.com/@alexeyskrobot/dsa-two-pointers-algorithm-review-with-the-step-by-step-guide-e8368e11a144)
50 changes: 50 additions & 0 deletions Uncategorized/Two Pointers/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { Tracer, Array1DTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');

const tracer = new Array1DTracer('Input array');
const areaTracer = new Array1DTracer('Max area calculation');
const logger = new LogTracer();

Layout.setRoot(new VerticalLayout([tracer, areaTracer, logger]));

function maxArea(height) {
tracer.set(height)

let left = 0;
let right = height.length - 1;
let maxArea = 0;

while (left < right) {
tracer.select(left);
tracer.select(right);
Tracer.delay();
logger.println(`Checking area between ${left} and ${right}`);

const h = Math.min(height[left], height[right]);
areaTracer.patch(0, h)
Tracer.delay();

const w = right - left;
areaTracer.patch(1, h)
Tracer.delay();

const area = h * w;
maxArea = Math.max(maxArea, area);

areaTracer.patch(2, maxArea)
Tracer.delay();

tracer.deselect(left, right)
Tracer.delay();

if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return maxArea;
}

const height1 = [1,8,6,2,5,4,8,3,7];
console.log(maxArea(height1)); // Output: 49
Binary file added Uncategorized/Two Pointers/img/question_11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.