diff --git a/Uncategorized/Two Pointers/README.md b/Uncategorized/Two Pointers/README.md new file mode 100644 index 00000000..1d8a6ccd --- /dev/null +++ b/Uncategorized/Two Pointers/README.md @@ -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) diff --git a/Uncategorized/Two Pointers/code.js b/Uncategorized/Two Pointers/code.js new file mode 100644 index 00000000..de32a7b3 --- /dev/null +++ b/Uncategorized/Two Pointers/code.js @@ -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 \ No newline at end of file diff --git a/Uncategorized/Two Pointers/img/question_11.jpg b/Uncategorized/Two Pointers/img/question_11.jpg new file mode 100644 index 00000000..7661efe4 Binary files /dev/null and b/Uncategorized/Two Pointers/img/question_11.jpg differ