We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent de3fdc9 commit e68120eCopy full SHA for e68120e
Easy/Minimum Sum of Mountain Triplets I.java
@@ -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