-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1918-KthSmallestSubarraySum.go
More file actions
104 lines (96 loc) · 3.1 KB
/
1918-KthSmallestSubarraySum.go
File metadata and controls
104 lines (96 loc) · 3.1 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
// 1918. Kth Smallest Subarray Sum
// Given an integer array nums of length n and an integer k, return the kth smallest subarray sum.
// A subarray is defined as a non-empty contiguous sequence of elements in an array.
// A subarray sum is the sum of all elements in the subarray.
// Example 1:
// Input: nums = [2,1,3], k = 4
// Output: 3
// Explanation: The subarrays of [2,1,3] are:
// - [2] with sum 2
// - [1] with sum 1
// - [3] with sum 3
// - [2,1] with sum 3
// - [1,3] with sum 4
// - [2,1,3] with sum 6
// Ordering the sums from smallest to largest gives 1, 2, 3, 3, 4, 6. The 4th smallest is 3.
// Example 2:
// Input: nums = [3,3,5,5], k = 7
// Output: 10
// Explanation: The subarrays of [3,3,5,5] are:
// - [3] with sum 3
// - [3] with sum 3
// - [5] with sum 5
// - [5] with sum 5
// - [3,3] with sum 6
// - [3,5] with sum 8
// - [5,5] with sum 10
// - [3,3,5], with sum 11
// - [3,5,5] with sum 13
// - [3,3,5,5] with sum 16
// Ordering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, 10, 11, 13, 16. The 7th smallest is 10.
// Constraints:
// n == nums.length
// 1 <= n <= 2 * 10^4
// 1 <= nums[i] <= 5 * 10^4
// 1 <= k <= n * (n + 1) / 2
import "fmt"
func kthSmallestSubarraySum(nums []int, k int) int {
res, start, end := 0, 1 << 31, 0
preSum := make([]int, len(nums) + 1)
min := func (x, y int) int { if x < y { return x; }; return y; }
for i := 1; i < len(preSum); i++ {
end += nums[i-1]
start = min(start, nums[i-1])
preSum[i] = preSum[i-1] + nums[i-1]
}
kthSmall := func(nums []int, mid, k int, preSum []int) bool {
count, low := 0, 0
for right := range preSum {
for preSum[right] - preSum[low] > mid { // 表示以right为右边界条件下满足和小于等于mid的子数组个数
low++
}
count += (right - low)
}
return count >= k
}
for start <= end {
mid := (start + end) / 2
if kthSmall(nums, mid, k, preSum) {
res, end = mid, mid - 1
} else {
start = mid + 1
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [2,1,3], k = 4
// Output: 3
// Explanation: The subarrays of [2,1,3] are:
// - [2] with sum 2
// - [1] with sum 1
// - [3] with sum 3
// - [2,1] with sum 3
// - [1,3] with sum 4
// - [2,1,3] with sum 6
// Ordering the sums from smallest to largest gives 1, 2, 3, 3, 4, 6. The 4th smallest is 3.
fmt.Println(kthSmallestSubarraySum([]int{2,1,3}, 4)) // 3
// Example 2:
// Input: nums = [3,3,5,5], k = 7
// Output: 10
// Explanation: The subarrays of [3,3,5,5] are:
// - [3] with sum 3
// - [3] with sum 3
// - [5] with sum 5
// - [5] with sum 5
// - [3,3] with sum 6
// - [3,5] with sum 8
// - [5,5] with sum 10
// - [3,3,5], with sum 11
// - [3,5,5] with sum 13
// - [3,3,5,5] with sum 16
// Ordering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, 10, 11, 13, 16. The 7th smallest is 10.
fmt.Println(kthSmallestSubarraySum([]int{3,3,5,5}, 7)) // 10
}