Repository navigation
Expand file tree
/
Copy pathSnowpack.java
More file actions
52 lines (46 loc) · 1.46 KB
/
Copy pathSnowpack.java
File metadata and controls
52 lines (46 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.dubey.leetcode;
import java.util.Arrays;
public class Snowpack {
/*
** Find the amount of snow that could be captured.
*/
public static Integer computeSnowpack(Integer[] arr) {
int snowCount = 0;
int[] arr1 = new int[arr.length];
int[] arr2 = new int[arr.length];
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
arr1[i] = max;
}
max = Integer.MIN_VALUE;
for (int i = arr.length - 1; i >= 0; i--) {
max = Math.max(max, arr[i]);
arr2[i] = max;
}
for (int i = 0; i < arr.length; i++) {
snowCount += (Math.min(arr1[i], arr2[i]) - arr[i]);
}
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
return snowCount;
}
/*
** Returns true if the tests pass. Otherwise, returns false;
*/
public static boolean doTestsPass() {
boolean result = true;
return computeSnowpack(new Integer[] {0, 1, 3, 0, 1, 2, 0, 4, 2, 0, 3, 0}) == 13;
// result &= computeSnowpack(new Integer[]{1, 8, 6, 2, 5, 4, 8, 3, 7}) == 49;
}
/*
** Execution entry point.
*/
public static void main(String[] args) {
if (doTestsPass()) {
System.out.println("All tests pass");
} else {
System.out.println("Tests fail.");
}
}
}