From 3cabba0a6a271198f05650713b486d3721f14dd6 Mon Sep 17 00:00:00 2001 From: Ashutosh Tiwari Date: Sun, 29 Sep 2024 19:00:16 -0500 Subject: [PATCH] ADD: trapping rain water problem --- .../Day7/trapping_rain_water.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 XDaysChallenge/90DaysChallenge/Day7/trapping_rain_water.cpp diff --git a/XDaysChallenge/90DaysChallenge/Day7/trapping_rain_water.cpp b/XDaysChallenge/90DaysChallenge/Day7/trapping_rain_water.cpp new file mode 100644 index 0000000..ab661eb --- /dev/null +++ b/XDaysChallenge/90DaysChallenge/Day7/trapping_rain_water.cpp @@ -0,0 +1,31 @@ +// https://leetcode.com/problems/trapping-rain-water/ + +/** + * Keeping leftMax and rightMax as a preprocessing step + * Focus on current index, and check if it can store water + */ +class Solution { +public: + int trap(vector& height) { + + vector left(height.size(), 0); + vector right(height.size(), 0); + int leftMax=0, rightMax=0; + + for(int i=0, j=height.size()-1;iheight[i]) { + waterStored += (maxCurrHeight - height[i]); + } + } + return waterStored; + } +}; \ No newline at end of file