File tree 2 files changed +46
-1
lines changed
2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 1
1
import java .util .*;
2
2
3
- class Calculator {
3
+ class BasicCalculator {
4
4
public static void main (String [] args ) {
5
5
Scanner s = new Scanner (System .in );
6
6
String str = s .nextLine ();
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int trap (int [] arr ) {
3
+
4
+ int n = arr .length ;
5
+
6
+ // initialize output
7
+ int result = 0 ;
8
+
9
+ // maximum element on left and right
10
+ int left_max = 0 , right_max = 0 ;
11
+
12
+ // indices to traverse the array
13
+ int lo = 0 , hi = n -1 ;
14
+
15
+ while (lo <= hi )
16
+ {
17
+ if (arr [lo ] < arr [hi ])
18
+ {
19
+ if (arr [lo ] > left_max )
20
+
21
+ // update max in left
22
+ left_max = arr [lo ];
23
+ else
24
+
25
+ // water on curr element =
26
+ // max - curr
27
+ result += left_max - arr [lo ];
28
+ lo ++;
29
+ }
30
+ else
31
+ {
32
+ if (arr [hi ] > right_max )
33
+
34
+ // update right maximum
35
+ right_max = arr [hi ];
36
+
37
+ else
38
+ result += right_max - arr [hi ];
39
+ hi --;
40
+ }
41
+ }
42
+
43
+ return result ;
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments