- Notifications
You must be signed in to change notification settings - Fork180
feat: added "Two Pointers" approach visualization#37
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
scrobot wants to merge2 commits intoalgorithm-visualizer:masterChoose a base branch fromscrobot:two-pointers
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletionsUncategorized/Two Pointers/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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/). | ||
 | ||
``` | ||
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 deletionsUncategorized/Two Pointers/code.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 addedUncategorized/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.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.