MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Trapping-rain-water/Solution.java:2–31  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1//Solution of traping rain water in java
2class Solution {
3 public int trap(int[] height) {
4 //create 2 temprory array
5 int a[] = new int[height.length];
6 int b[] = new int[height.length];
7 int min = Integer.MIN_VALUE;
8 //first we are finfig min hight while traversing height array form left to right
9 for(int i=0;i<height.length;i++){
10 if(min<height[i]){
11 min=height[i];
12 }
13 a[i]=min;
14 }
15 //fnow we are finfig min hight while traversing height array form right to rleft
16 min = Integer.MIN_VALUE;
17 for(int i=height.length-1;i>=0;i--){
18 if(min<height[i]){
19 min=height[i];
20 }
21 b[i]=min;
22 }
23 int total_water=0;
24 //at last we will add min number from temrory array and minus hight which is not coming in use while storing water
25 for(int i=0;i<height.length;i++){
26 total_water+= Math.min(a[i],b[i])-height[i];
27 }
28 return total_water;
29
30 }
31}
32

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected