-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2908-MinimumSumOfMountainTripletsI.go
More file actions
104 lines (91 loc) · 3.4 KB
/
2908-MinimumSumOfMountainTripletsI.go
File metadata and controls
104 lines (91 loc) · 3.4 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
// 2908. 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:
// Input: nums = [8,6,1,5,3]
// Output: 9
// Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
// - 2 < 3 < 4
// - nums[2] < nums[3] and nums[4] < nums[3]
// 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:
// - 1 < 3 < 5
// - nums[1] < nums[3] and nums[5] < nums[3]
// 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:
// 3 <= nums.length <= 50
// 1 <= nums[i] <= 50
import "fmt"
func minimumSum(nums []int) int {
res := int(1e9)
min := func (a int, b int) int { if a < b { return a; }; return b; }
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
for k := j + 1; k < len(nums); k++ {
if nums[i] < nums[j] && nums[k] < nums[j] {
res = min(res,nums[i] + nums[j] + nums[k])
}
}
}
}
if res == int(1e9) {
return -1
}
return res
}
func minimumSum1(nums []int) int {
res := int(1e9)
post, pre := make([]int, len(nums)), make([]int, len(nums))
min := func (a int, b int) int { if a < b { return a; }; return b; }
// 后缀最小值
post[len(nums)-1] = nums[len(nums)-1]
for i := len(nums)-2; i >= 0; i-- {
post[i] = min(post[i+1], nums[i])
}
// 前缀最小值
pre[0] = nums[0]
for i := 1; i < len(nums); i++ {
pre[i] = min(pre[i-1], nums[i])
}
for i := 1; i < len(nums) - 1; i++ {
if nums[i] > pre[i-1] && nums[i] > post[i+1]{
res = min(res, nums[i] + pre[i-1] + post[i+1])
}
}
if int(1e9) == res {
return -1
}
return res
}
func main() {
// Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
// - 2 < 3 < 4
// - nums[2] < nums[3] and nums[4] < nums[3]
// 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.
fmt.Println(minimumSum([]int{8,6,1,5,3})) // 9
// Explanation: Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
// - 1 < 3 < 5
// - nums[1] < nums[3] and nums[5] < nums[3]
// 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.
fmt.Println(minimumSum([]int{5,4,8,7,10,2})) // 13
// Explanation: It can be shown that there are no mountain triplets in nums.
fmt.Println(minimumSum([]int{6,5,4,3,4,5})) // -1
fmt.Println(minimumSum1([]int{8,6,1,5,3})) // 9
fmt.Println(minimumSum1([]int{5,4,8,7,10,2})) // 13
fmt.Println(minimumSum1([]int{6,5,4,3,4,5})) // -1
}