Tuesday 24 August 2021

trapping-rain-water

 https://leetcode.com/problems/trapping-rain-water/


brute-force 

water at any x = min( max_left_of_x, max_right_of_x) - height_of_x

two-pointer solution 

when keeping 

1) 

  • p_start, at 0
  • p_end at 11 [length-1] of array
  • check, 
    • p_start height is less. => 0
    • p_end height is less => 1
      • answer: p_start => 0 is lesser
  • height at 0 is less
  • p_start becomes our p_current pointer.
  • with respect to p_current
  • we check 
    • max_left
    • max_right  
  1. whenever we check which is lesser p_start, p_end, we are actually evaluating  min(p_start, p_end)
  2. by checking min, of two, we get the level, which water can be filled.

formula

 

Level => check min of p_start, p_end 

current_level = min(p_start, p_end)

if p_start is less, we start from left, and 

  • make max_left = height_at_p_start 
  • make max_right = height_at_p_end
  • we consider level = max_left 
  •  



***incomplete post


 

 

 



container-with-most-water

 https://leetcode.com/problems/container-with-most-water/