-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMinimumSumOfMountainTripletsI.java
40 lines (34 loc) · 1.15 KB
/
MinimumSumOfMountainTripletsI.java
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
// https://leetcode.com/problems/minimum-sum-of-mountain-triplets-i
// T: O(N^2)
// T: O(1)
public class MinimumSumOfMountainTripletsI {
public int minimumSum(int[] array) {
int minSum = Integer.MAX_VALUE;
for (int i = 1 ; i < array.length - 1 ; i++) {
final int minLeft = getSmallestOnLeft(array, i);
if (minLeft >= array[i]) {
continue;
}
final int minRight = getSmallestOnRight(array, i);
if (minRight >= array[i]) {
continue;
}
minSum = Math.min(minSum, minLeft + minRight + array[i]);
}
return minSum == Integer.MAX_VALUE ? -1 : minSum;
}
private static int getSmallestOnLeft(int[] array, int index) {
int min = Integer.MAX_VALUE;
for (int i = 0 ; i < index ; i++) {
min = Math.min(min, array[i]);
}
return min;
}
private static int getSmallestOnRight(int[] array, int index) {
int min = Integer.MAX_VALUE;
for (int i = index + 1 ; i < array.length ; i++) {
min = Math.min(min, array[i]);
}
return min;
}
}