Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

Day 16/75 :

Problem : Trapping Rain Water -(Hard)

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.        

Approach1: Monotonic Stack Approach ( leftMax and RightMax)

Monotonic Stack: Stack which maintains , element is specific order. (ascending/descending)

Here, We maintain two different Arrays for leftMax and rightMax,at each position.

  1. Traverse left to right and find leftMax of each position ,store it in leftMax array.
  2. Now, traverse right to left and find rightMaxof each position ,store it in rightMax array.
  3. Now,Traverse again and calculate waterlevel at each position and return totalTrapped Water.


Code:

Time Complexity : O(n)

Space Complexity : O(n) , As we maintain two different array for leftMax and rightMax.

Approach 2: Two Pointer

We bascically maintains two pointers left (assigned to leftMost height)and right(assigned to rightMost height).

  1. Loop, till left less than equal to right.
  2. If height of left/right is greater than leftMax/ rightMax. Update leMax/rightMax and shift left and right pointer accordingly.
  3. But if ,leftMax/ rightMax is greater than height of left/right. Then simply ,calculate WaterLevel of that position. i.e ( waterlevel += min(lMax,rMax)-height[left/right].
  4. Return Trapped waterLevel.

Code:

Time Complexity: O(n)

Space Complexity: O(1) .As no extra space is used.


要查看或添加评论,请登录

Suyash Awathe的更多文章

  • Tree: Add One Row to Tree

    Tree: Add One Row to Tree

    Day 17: Problem Statement: Given the root of a binary tree and two integers val and depth, add a row of nodes with…

  • Stack: April POTD

    Stack: April POTD

    Question: Remove K Digits -(Medium) Given string num representing a non-negative integer num, and an integer k, return…

  • Queue-Array: April POTD

    Queue-Array: April POTD

    Day 14/75: Question: Reveal Cards In Increasing Order -(Medium) You are given an integer array . There is a deck of…

    1 条评论
  • Array: 9 April POTD

    Array: 9 April POTD

    Day 13/75: Question: Time Needed to Buy Tickets -(Easy) There are people in a line queuing to buy tickets, where the…

  • Queue : April POTD

    Queue : April POTD

    Day 12/75: Question: Number of Students Unable to Eat Lunch -(Easy) The school cafeteria offers circular and square…

    1 条评论
  • String : Parentheses : April-POTD

    String : Parentheses : April-POTD

    Day 11/75: Question: Minimum Remove to Make Valid Parentheses -(Medium) Given a string s of , and lowercase English…

    3 条评论
  • String : 5April POTD

    String : 5April POTD

    Day 10/75: Question: Make The String Great -(Easy) Given a string of lower and upper case English letters. A good…

  • String Parentheses : 4April POTD

    String Parentheses : 4April POTD

    Day 9/75: Question: Maximum Nesting Depth of the Parentheses -(Easy) A string is a valid parentheses string (denoted…

  • BackTracking : WordSearch :4April POTD

    BackTracking : WordSearch :4April POTD

    Day 9/75: Question: Word Search -(Medium) Given an m x n grid of characters board and a string word, return true if…

    1 条评论
  • Isomorphic String : April POTD

    Isomorphic String : April POTD

    Day 8/75: Question: Given two strings s and t, determine if they are isomorphic. Two strings and are isomorphic if the…

    2 条评论

社区洞察

其他会员也浏览了