-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3788-MaximumScoreOfASplit.go
More file actions
80 lines (65 loc) · 2.32 KB
/
3788-MaximumScoreOfASplit.go
File metadata and controls
80 lines (65 loc) · 2.32 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
package main
// 3788. Maximum Score of a Split
// You are given an integer array nums of length n.
// Choose an index i such that 0 <= i < n - 1.
// For a chosen split index i:
// Let prefixSum(i) be the sum of nums[0] + nums[1] + ... + nums[i].
// Let suffixMin(i) be the minimum value among nums[i + 1], nums[i + 2], ..., nums[n - 1].
// The score of a split at index i is defined as:
// score(i) = prefixSum(i) - suffixMin(i)
// Return an integer denoting the maximum score over all valid split indices.
// Example 1:
// Input: nums = [10,-1,3,-4,-5]
// Output: 17
// Explanation:
// The optimal split is at i = 2, score(2) = prefixSum(2) - suffixMin(2) = (10 + (-1) + 3) - (-5) = 17.
// Example 2:
// Input: nums = [-7,-5,3]
// Output: -2
// Explanation:
// The optimal split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = (-7) - (-5) = -2.
// Example 3:
// Input: nums = [1,1]
// Output: 0
// Explanation:
// The only valid split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = 1 - 1 = 0.
// Constraints:
// 2 <= nums.length <= 10^5
// -10^9 <= nums[i] <= 10^9
import "fmt"
// time: O(n), space: O(1)
func maximumScore(nums []int) int64 {
sum, res, mn := 0, -1 << 61, 1 << 61
for _, v := range nums {
sum += v
}
for i := len(nums) - 1; i > 0; i-- {
sum -= nums[i]
mn = min(mn, nums[i])
res = max(res, sum - mn)
}
return int64(res)
}
func main() {
// Example 1:
// Input: nums = [10,-1,3,-4,-5]
// Output: 17
// Explanation:
// The optimal split is at i = 2, score(2) = prefixSum(2) - suffixMin(2) = (10 + (-1) + 3) - (-5) = 17.
fmt.Println(maximumScore([]int{10,-1,3,-4,-5})) // 17
// Example 2:
// Input: nums = [-7,-5,3]
// Output: -2
// Explanation:
// The optimal split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = (-7) - (-5) = -2.
fmt.Println(maximumScore([]int{-7,-5,3})) // -2
// Example 3:
// Input: nums = [1,1]
// Output: 0
// Explanation:
// The only valid split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = 1 - 1 = 0.
fmt.Println(maximumScore([]int{1,1})) // 0
fmt.Println(maximumScore([]int{-7,-5,3})) // -2
fmt.Println(maximumScore([]int{1,2,3,4,5,6,7,8,9})) // 27
fmt.Println(maximumScore([]int{9,8,7,6,5,4,3,2,1})) // 43
}