Skip to content

Commit e68120e

Browse files
authored
Create Minimum Sum of Mountain Triplets I.java
1 parent de3fdc9 commit e68120e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minimumSum(int[] nums) {
3+
int n = nums.length;
4+
int[] smallestRight = new int[n];
5+
smallestRight[n - 1] = nums[n - 1];
6+
for (int i = n - 2; i >= 0; i--) {
7+
smallestRight[i] = Math.min(smallestRight[i + 1], nums[i]);
8+
}
9+
int smallestLeft = nums[0];
10+
int result = Integer.MAX_VALUE;
11+
for (int i = 1; i < n; i++) {
12+
if (smallestLeft < nums[i] && smallestRight[i] < nums[i]) {
13+
result = Math.min(result, nums[i] + smallestRight[i] + smallestLeft);
14+
}
15+
smallestLeft = Math.min(smallestLeft, nums[i]);
16+
}
17+
return result == Integer.MAX_VALUE ? -1 : result;
18+
}
19+
}

0 commit comments

Comments
 (0)