From 2f8b1881c1b0d920576d0b13a82751547be47633 Mon Sep 17 00:00:00 2001 From: RITIK PATIDAR <101919743+ritikpatel17@users.noreply.github.com> Date: Mon, 31 Oct 2022 22:52:53 +0530 Subject: [PATCH] Trapping Rain Water --- trap.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 trap.java diff --git a/trap.java b/trap.java new file mode 100644 index 0000000..8bf7870 --- /dev/null +++ b/trap.java @@ -0,0 +1,28 @@ +class Solution { + public int trap(int[] height) { + int []rightGtr = new int[height.length]; + int []leftGtr = new int[height.length]; + //left greater + for(int i=0; i=0; i--) { + if(i==rightGtr.length-1) rightGtr[i]=height[i]; + else + rightGtr[i] = Math.max(rightGtr[i+1], height[i]); + } + // System.out.println(Arrays.toString(rightGtr)); + int water =0; + for(int i=0; i=height[i]) + water += level-height[i]; + + } + + return water; + } +}