forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-total-beauty-of-the-gardens.py
164 lines (145 loc) · 5.03 KB
/
maximum-total-beauty-of-the-gardens.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Time: O(nlogn)
# Space: O(1)
import bisect
# sort, prefix sum, greedy, two pointers, improved from solution3
class Solution(object):
def maximumBeauty(self, flowers, newFlowers, target, full, partial):
"""
:type flowers: List[int]
:type newFlowers: int
:type target: int
:type full: int
:type partial: int
:rtype: int
"""
flowers.sort()
n = bisect.bisect_left(flowers, target)
prefix, suffix = 0, sum(flowers[i] for i in xrange(n))
result = left = 0
for right in xrange(n+1):
if right:
suffix -= flowers[right-1]
total = newFlowers-((n-right)*target-suffix)
if total < 0:
continue
while not (left == right or (left and (total+prefix)//left <= flowers[left])):
prefix += flowers[left]
left += 1
mn = min((total+prefix)//left if left else 0, target-1)
result = max(result, mn*partial+(len(flowers)-right)*full)
return result
# Time: O(nlogn)
# Space: O(1)
import bisect
# sort, prefix sum, greedy, two pointers, improved from solution4
class Solution2(object):
def maximumBeauty(self, flowers, newFlowers, target, full, partial):
"""
:type flowers: List[int]
:type newFlowers: int
:type target: int
:type full: int
:type partial: int
:rtype: int
"""
flowers.sort()
n = bisect.bisect_left(flowers, target)
prefix = [0]*(n+1)
for i in xrange(n):
prefix[i+1] = prefix[i]+flowers[i]
result = suffix = 0
left = n
for right in reversed(xrange(n+1)):
if right != n:
suffix += flowers[right]
total = newFlowers-((n-right)*target-suffix)
if total < 0:
continue
left = min(left, right)
while not (left == 0 or (prefix[left]-prefix[left-1])*left-prefix[left] <= total):
left -= 1
mn = min((total+prefix[left])//left if left else 0, target-1)
result = max(result, mn*partial+(len(flowers)-right)*full)
return result
# Time: O(nlogn)
# Space: O(n)
import bisect
# sort, prefix sum, greedy, binary search
class Solution3(object):
def maximumBeauty(self, flowers, newFlowers, target, full, partial):
"""
:type flowers: List[int]
:type newFlowers: int
:type target: int
:type full: int
:type partial: int
:rtype: int
"""
def check(prefix, total, x):
return x and (total+prefix[x])//x <= prefix[x+1]-prefix[x]
def binary_search(prefix, total, left, right):
while left <= right:
mid = left+(right-left)//2
if check(prefix, total, mid):
right = mid-1
else:
left = mid+1
return left
flowers.sort()
n = bisect.bisect_left(flowers, target)
prefix = [0]*(n+1)
for i in xrange(n):
prefix[i+1] = prefix[i]+flowers[i]
suffix = sum(flowers[i] for i in xrange(n))
result = left = 0
for right in xrange(n+1):
if right:
suffix -= flowers[right-1]
total = newFlowers-((n-right)*target-suffix)
if total < 0:
continue
left = binary_search(prefix, total, 0, right-1)
mn = min((total+prefix[left])//left if left else 0, target-1)
result = max(result, mn*partial+(len(flowers)-right)*full)
return result
# Time: O(nlogn)
# Space: O(n)
import bisect
# sort, prefix sum, greedy, binary search
class Solution4(object):
def maximumBeauty(self, flowers, newFlowers, target, full, partial):
"""
:type flowers: List[int]
:type newFlowers: int
:type target: int
:type full: int
:type partial: int
:rtype: int
"""
def check(prefix, total, x):
return (prefix[x]-prefix[x-1])*x-prefix[x] <= total
def binary_search_right(prefix, total, left, right):
while left <= right:
mid = left+(right-left)//2
if not check(prefix, total, mid):
right = mid-1
else:
left = mid+1
return right
flowers.sort()
n = bisect.bisect_left(flowers, target)
prefix = [0]*(n+1)
for i in xrange(n):
prefix[i+1] = prefix[i]+flowers[i]
result = suffix = 0
left = n
for right in reversed(xrange(n+1)):
if right != n:
suffix += flowers[right]
total = newFlowers-((n-right)*target-suffix)
if total < 0:
break
left = binary_search_right(prefix, total, 1, right)
mn = min((total+prefix[left])//left if left else 0, target-1)
result = max(result, mn*partial+(len(flowers)-right)*full)
return result