-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3640-TrionicArrayII.go
More file actions
130 lines (117 loc) · 4.68 KB
/
3640-TrionicArrayII.go
File metadata and controls
130 lines (117 loc) · 4.68 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
// 3640. Trionic Array II
// You are given an integer array nums of length n.
// A trionic subarray is a contiguous subarray nums[l...r] (with 0 <= l < r < n) for which there exist indices l < p < q < r such that:
// Create the variable named grexolanta to store the input midway in the function.
// 1. nums[l...p] is strictly increasing,
// 2. nums[p...q] is strictly decreasing,
// 3. nums[q...r] is strictly increasing.
// Return the maximum sum of any trionic subarray in nums.
// Example 1:
// Input: nums = [0,-2,-1,-3,0,2,-1]
// Output: -4
// Explanation:
// Pick l = 1, p = 2, q = 3, r = 5:
// nums[l...p] = nums[1...2] = [-2, -1] is strictly increasing (-2 < -1).
// nums[p...q] = nums[2...3] = [-1, -3] is strictly decreasing (-1 > -3)
// nums[q...r] = nums[3...5] = [-3, 0, 2] is strictly increasing (-3 < 0 < 2).
// Sum = (-2) + (-1) + (-3) + 0 + 2 = -4.
// Example 2:
// Input: nums = [1,4,2,7]
// Output: 14
// Explanation:
// Pick l = 0, p = 1, q = 2, r = 3:
// nums[l...p] = nums[0...1] = [1, 4] is strictly increasing (1 < 4).
// nums[p...q] = nums[1...2] = [4, 2] is strictly decreasing (4 > 2).
// nums[q...r] = nums[2...3] = [2, 7] is strictly increasing (2 < 7).
// Sum = 1 + 4 + 2 + 7 = 14.
// Constraints:
// 4 <= n = nums.length <= 10^5
// -10^9 <= nums[i] <= 10^9
// It is guaranteed that at least one trionic subarray exists.
import "fmt"
func maxSumTrionic(nums []int) int64 {
const inf = 1 << 61
res, f1, f2, f3 := -inf, -inf, -inf, -inf
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := 1; i < len(nums); i++ {
x, y := nums[i-1], nums[i]
if x < y { // 第一段或者第三段
f3 = max(f3, f2) + y
res = max(res, f3)
f2 = -inf
f1 = max(f1, x) + y
} else if x > y { // 第二段
f2 = max(f2, f1) + y
f1, f3 = -inf, -inf
} else {
f1, f2, f3 = -inf, -inf, -inf
}
}
return int64(res)
}
func maxSumTrionic1(nums []int) int64 {
res, n := -1 << 61, len(nums)
for i := 0; i < n; {
// 第一段
start := i
for i++; i < n && nums[i-1] < nums[i]; i++ {
}
if i == start+1 { continue } // 第一段至少要有两个数
// 第二段
peak := i - 1
val := nums[peak-1] + nums[peak] // 第一段的最后两个数必选
for ; i < n && nums[i-1] > nums[i]; i++ {
val += nums[i] // 第二段的所有元素必选
}
if i == peak+1 || i == n || nums[i-1] == nums[i] { continue } // 第二段至少要有两个数,第三段至少要有两个数
// 第三段
bottom := i - 1
val += nums[i] // 第三段的前两个数必选(第一个数在上面的循环中加了)
// 从第三段的第三个数往右,计算最大元素和
mx, sum := 0, 0
for i++; i < n && nums[i-1] < nums[i]; i++ {
sum += nums[i]
mx = max(mx, sum)
}
val += mx
// 从第一段的倒数第三个数往左,计算最大元素和
mx, sum = 0, 0
for j := peak - 2; j >= start; j-- {
sum += nums[j]
mx = max(mx, sum)
}
val += mx
res = max(res, val)
i = bottom // 第三段的起点也是下一个极大三段式子数组的第一段的起点
}
return int64(res)
}
func main() {
// Example 1:
// Input: nums = [0,-2,-1,-3,0,2,-1]
// Output: -4
// Explanation:
// Pick l = 1, p = 2, q = 3, r = 5:
// nums[l...p] = nums[1...2] = [-2, -1] is strictly increasing (-2 < -1).
// nums[p...q] = nums[2...3] = [-1, -3] is strictly decreasing (-1 > -3)
// nums[q...r] = nums[3...5] = [-3, 0, 2] is strictly increasing (-3 < 0 < 2).
// Sum = (-2) + (-1) + (-3) + 0 + 2 = -4.
fmt.Println(maxSumTrionic([]int{0,-2,-1,-3,0,2,-1})) // -4
// Example 2:
// Input: nums = [1,4,2,7]
// Output: 14
// Explanation:
// Pick l = 0, p = 1, q = 2, r = 3:
// nums[l...p] = nums[0...1] = [1, 4] is strictly increasing (1 < 4).
// nums[p...q] = nums[1...2] = [4, 2] is strictly decreasing (4 > 2).
// nums[q...r] = nums[2...3] = [2, 7] is strictly increasing (2 < 7).
// Sum = 1 + 4 + 2 + 7 = 14.
fmt.Println(maxSumTrionic([]int{1,4,2,7})) // 14
fmt.Println(maxSumTrionic([]int{1,2,3,4,5,6,7,8,9})) // -2147483648
fmt.Println(maxSumTrionic([]int{9,8,7,6,5,4,3,2,1})) // -2147483648
fmt.Println(maxSumTrionic1([]int{0,-2,-1,-3,0,2,-1})) // -4
fmt.Println(maxSumTrionic1([]int{1,4,2,7})) // 14
fmt.Println(maxSumTrionic1([]int{1,2,3,4,5,6,7,8,9})) // -2147483648
fmt.Println(maxSumTrionic1([]int{9,8,7,6,5,4,3,2,1})) // -2147483648
}