From 1829f74dbfcf0102db10093dd71bf87568988efd Mon Sep 17 00:00:00 2001 From: Manisha Kundnani <90680330+Manishak798@users.noreply.github.com> Date: Thu, 21 Nov 2024 13:55:07 +0530 Subject: [PATCH] Create tappingrainwater.cpp --- OOPS/tappingrainwater.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 OOPS/tappingrainwater.cpp diff --git a/OOPS/tappingrainwater.cpp b/OOPS/tappingrainwater.cpp new file mode 100644 index 0000000..539eac8 --- /dev/null +++ b/OOPS/tappingrainwater.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int trap(vector& height) { + int lMax = 0; + int rMax = 0; + int total = 0; + int left = 0; + int right = height.size()-1; + while(left < right){ + if(height[left] <= height[right]){ + if(lMax > height[left]){ + total += lMax-height[left]; + }else{ + lMax = height[left]; + } + left = left+1; + } + else{ + if(rMax > height[right]){ + total += rMax - height[right]; + }else{ + rMax = height[right]; + } + right = right - 1; + } + } + return total; + } +};