JuniorArray

Minimum Sum of Mountain Triplets I

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

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

  • i < j < k
  • nums[i] < nums[j] and nums[k] < nums[j]

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

Example 1
#
#
# #
# #   #
# #   #
# #   # #
# #   # #
# # # # #
8 6 1 5 3
Inputnums = [8,6,1,5,3]
Output9
Triplet (2, 3, 4) is a mountain triplet with sum nums[2] + nums[3] + nums[4] = 9, and no mountain triplet has a smaller sum.
Example 2
             #
             #
       #     #
       #  #  #
       #  #  #
 #     #  #  #
 #  #  #  #  #
 #  #  #  #  #
 #  #  #  #  #  #
 #  #  #  #  #  #
 5  4  8  7 10  2
Inputnums = [5,4,8,7,10,2]
Output13
Triplet (1, 3, 5) is a mountain triplet with sum nums[1] + nums[3] + nums[5] = 13, and no mountain triplet has a smaller sum.

Constraints

  • 3 <= nums.length <= 50
  • 1 <= nums[i] <= 50

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate