-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path3736.kth-smallest-subarray-sum.py
84 lines (78 loc) · 1.98 KB
/
3736.kth-smallest-subarray-sum.py
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
# Tag: Binary Search, Two Pointers, Array
# Time: O(NlogA)
# Space: O(1)
# Ref: Leetcode-1918
# Note: -
# Given an array of positive integers `n` of length `nums` and an integer `k`, return the sum of **the $k^{th}$ smallest subarray of `nums`.**
#
# Example 1:
# ```
# Input:
# nums = [1, 2, 3]
# k = 4
# Output:
# 3
# Explanation:
# The subarrays of [1, 2, 3] are:
# [1], [2], [3]
# [1, 2], [2, 3]
# [1, 2, 3]
# The subarray sums are in ascending order: 1, 2, 3, 3, 5, 6
# The fourth smallest subarray sum is 3
# ```
#
# Example 2:
# ```
# Input:
# nums = [2, 2, 6, 6]
# k = 6
# Output:
# 8
# Explanation:
# The subarrays of [2, 2, 6, 6] are:
# [2], [2], [6], [6]
# [2, 2], [2, 6], [6, 6]
# [2, 2, 6], [2, 6, 6]
# [2, 2, 6, 6]
# The subarray sums are in ascending order: 2, 2, 4, 6, 6, 8, 10, 12, 14, 16
# The sixth smallest subarray sum is 8
#
# ```
#
# $1 \leq nums.length \leq 10 ^4$
# $1 \leq nums[i] \leq 5 * 10^4$
# $1 \leq k \leq n * (n + 1) \ / \ 2$
# In an array, a subarray is formed from one or more consecutive array elements. The subarray should contain at least one element
from typing import (
List,
)
import heapq
class Solution:
"""
@param nums: An integer array
@param k: A positive integer
@return: Return the kth smallest subarray sum
"""
def kth_smallest_subarray_sum(self, nums: List[int], k: int) -> int:
# write your code here
left = min(nums)
right = sum(nums)
while left < right:
mid = left + (right - left) // 2
if self.count(nums, mid) < k:
left = mid + 1
else:
right = mid
return left
def count(self, nums: list, target: int) -> int:
n = len(nums)
count = 0
i = 0
res = 0
for j in range(n):
count += nums[j]
while count > target:
count -= nums[i]
i += 1
res += j - i + 1
return res