LeetCode-in-Java

2909. Minimum Sum of Mountain Triplets II

Medium

You are given a 0-indexed array nums of integers.

A triplet of indices (i, j, k) is a mountain if:

Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.

Example 1:

Input: nums = [8,6,1,5,3]

Output: 9

Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since:

And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.

Example 2:

Input: nums = [5,4,8,7,10,2]

Output: 13

Explanation: Triplet (1, 3, 5) is a mountain triplet of sum 13 since:

And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.

Example 3:

Input: nums = [6,5,4,3,4,5]

Output: -1

Explanation: It can be shown that there are no mountain triplets in nums.

Constraints:

Solution

public class Solution {
    public int minimumSum(int[] nums) {
        int n = nums.length;
        int[] leftSmallest = new int[n];
        int[] rightSmallest = new int[n];
        int currSmallest = nums[0];
        leftSmallest[0] = -1;
        for (int i = 1; i < n; i++) {
            if (currSmallest >= nums[i]) {
                leftSmallest[i] = -1;
                currSmallest = nums[i];
            } else {
                leftSmallest[i] = currSmallest;
            }
        }
        currSmallest = nums[n - 1];
        rightSmallest[n - 1] = -1;
        for (int i = n - 2; i >= 0; i--) {
            if (currSmallest >= nums[i]) {
                rightSmallest[i] = -1;
                currSmallest = nums[i];
            } else {
                rightSmallest[i] = currSmallest;
            }
        }
        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            if (leftSmallest[i] != -1 && rightSmallest[i] != -1) {
                ans = Math.min(ans, leftSmallest[i] + rightSmallest[i] + nums[i]);
            }
        }
        if (ans == Integer.MAX_VALUE) {
            return -1;
        }
        return ans;
    }
}