-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2789-LargestElementInAnArrayAfterMergeOperations.go
More file actions
95 lines (82 loc) · 3.38 KB
/
2789-LargestElementInAnArrayAfterMergeOperations.go
File metadata and controls
95 lines (82 loc) · 3.38 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
package main
// 2789. Largest Element in an Array after Merge Operations
// You are given a 0-indexed array nums consisting of positive integers.
// You can do the following operation on the array any number of times:
// Choose an integer i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1].
// Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array.
// Return the value of the largest element that you can possibly obtain in the final array.
// Example 1:
// Input: nums = [2,3,7,9,3]
// Output: 21
// Explanation: We can apply the following operations on the array:
// - Choose i = 0. The resulting array will be nums = [5,7,9,3].
// - Choose i = 1. The resulting array will be nums = [5,16,3].
// - Choose i = 0. The resulting array will be nums = [21,3].
// The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.
// Example 2:
// Input: nums = [5,3,3]
// Output: 11
// Explanation: We can do the following operations on the array:
// - Choose i = 1. The resulting array will be nums = [5,6].
// - Choose i = 0. The resulting array will be nums = [11].
// There is only one element in the final array, which is 11.
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^6
import "fmt"
func maxArrayValue(nums []int) int64 {
lastValue, lastMax := nums[len(nums) - 1], nums[len(nums) - 1]
for i := len(nums) - 2; i >= 0; i-- {
// 如果当前值 >= 进行累加
if nums[i] <= lastValue {
lastValue += nums[i]
} else { // 否则替换
lastValue = nums[i]
}
// 如果最大值 > 最后值 替换
if lastValue > lastMax {
lastMax = lastValue
}
}
return int64(lastMax)
}
func maxArrayValue1(nums []int) int64 {
sum := int64(nums[len(nums)-1])
for i := len(nums) - 2; i >= 0; i-- {
if int64(nums[i]) <= sum {
sum = int64(nums[i]) + sum
} else {
sum = int64(nums[i])
}
}
return sum
}
func maxArrayValue2(nums []int) int64 {
sum := nums[len(nums) - 1]
// 在逆序遍历的过程中可以保证记录到每次连续合并的最大值
for i := len(nums) - 2; i >= 0; i-- {
if nums[i] <= sum {
sum = nums[i] + sum
} else {
sum = nums[i]
}
}
return int64(sum)
}
func main() {
// Explanation: We can apply the following operations on the array:
// - Choose i = 0. The resulting array will be nums = [5,7,9,3].
// - Choose i = 1. The resulting array will be nums = [5,16,3].
// - Choose i = 0. The resulting array will be nums = [21,3].
// The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.
fmt.Println(maxArrayValue([]int{ 2,3,7,9,3 })) // 21
// Explanation: We can do the following operations on the array:
// - Choose i = 1. The resulting array will be nums = [5,6].
// - Choose i = 0. The resulting array will be nums = [11].
// There is only one element in the final array, which is 11.
fmt.Println(maxArrayValue([]int{ 5,3,3 })) // 11
fmt.Println(maxArrayValue1([]int{ 2,3,7,9,3 })) // 21
fmt.Println(maxArrayValue1([]int{ 5,3,3 })) // 11
fmt.Println(maxArrayValue2([]int{ 2,3,7,9,3 })) // 21
fmt.Println(maxArrayValue2([]int{ 5,3,3 })) // 11
}