-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlongest-subsequence-with-limited-sum.py
91 lines (82 loc) · 3.02 KB
/
longest-subsequence-with-limited-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
85
86
87
88
89
90
91
# 2389. Longest Subsequence With Limited Sum
# 🟢 Easy
#
# https://leetcode.com/problems/longest-subsequence-with-limited-sum/
#
# Tags: Array - Binary Search - Greedy - Sorting - Prefix Sum
import timeit
from bisect import bisect_right
from itertools import accumulate
from typing import List
# Create an array of prefix sums of the sorted input nums, result[i]
# will be the index at which we would need to insert that value on the
# prefix sum array to maintain the sorted order. What the algorithm does
# is to greedily pick the highest number of values that gives the lowest
# possible subsequence sum, then use binary search to determine how
# many values we can take while remaining below the given sum value.
#
# Time complexity: O(n*log(n) + k*log(n)) - Where n is the number of
# elements in the nums array and k is the number of queries, we need to
# sort the array nums in n*log(n) time, and compute the prefix sums in
# O(n), then, for each query k, we use binary search in O(log(n)) time
# to find the minimum number of values we can sum while still remaining
# below the query value.
# Space complexity: O(n) - The prefix sum array has the same size as the
# nums array.
#
# Runtime 99 ms Beats 98.35%
# Memory 14.1 MB Beats 79.61%
class Pythonic:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
prefix_sums = [ps for ps in accumulate(sorted(nums))]
return [bisect_right(prefix_sums, query) for query in queries]
# Same logic as the previous solution but avoiding the use of any
# libraries.
#
# Time complexity: O(n*log(n) + k*log(n)).
# Space complexity: O(n).
#
# Runtime 118 ms Beats 90.49%
# Memory 14.1 MB Beats 79.61%
class Custom:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
prefix_sums, n, m = sorted(nums), len(nums), len(queries)
for i in range(1, n):
prefix_sums[i] += prefix_sums[i - 1]
res = [None] * m
for i in range(m):
l, r = 0, n
while l < r:
mid = (l + r) // 2
if prefix_sums[mid] <= queries[i]:
l = mid + 1
else:
r = mid
res[i] = l
return res
def test():
executors = [
Pythonic,
Custom,
]
tests = [
[[2, 3, 4, 5], [1], [0]],
[[4, 5, 2, 1], [3, 10, 21], [2, 3, 4]],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.answerQueries(t[0], t[1])
exp = t[2]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()