Skip to content

Commit 8b39092

Browse files
committed
Added some more Hard questions 🎉
1 parent 86e0d8d commit 8b39092

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Hard/Calculator.java renamed to Hard/BasicCalculator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import java.util.*;
22

3-
class Calculator {
3+
class BasicCalculator {
44
public static void main(String[] args) {
55
Scanner s = new Scanner(System.in);
66
String str = s.nextLine();

Hard/TrappingRainWater.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)