Skip to content

Commit 2847576

Browse files
authored
Create number-of-flowers-in-full-bloom.py
1 parent 3d8b353 commit 2847576

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Time: O(nlogn + mlogn)
2+
# Space: O(n)
3+
4+
import bisect
5+
6+
7+
# line sweep, binary search
8+
class Solution(object):
9+
def fullBloomFlowers(self, flowers, persons):
10+
"""
11+
:type flowers: List[List[int]]
12+
:type persons: List[int]
13+
:rtype: List[int]
14+
"""
15+
cnt = collections.Counter()
16+
for s, e in flowers:
17+
cnt[s] += 1
18+
cnt[e+1] -= 1
19+
events = sorted(cnt.iterkeys())
20+
prefix = [0]
21+
for x in events:
22+
prefix.append(prefix[-1]+cnt[x])
23+
return [prefix[bisect.bisect_right(events, t)] for t in persons]
24+
25+
26+
# Time: O(nlogn + mlogn)
27+
# Space: O(n)
28+
import bisect
29+
30+
31+
# binary search
32+
class Solution(object):
33+
def fullBloomFlowers(self, flowers, persons):
34+
"""
35+
:type flowers: List[List[int]]
36+
:type persons: List[int]
37+
:rtype: List[int]
38+
"""
39+
starts, ends = [], []
40+
for s, e in flowers:
41+
starts.append(s)
42+
ends.append(e+1)
43+
starts.sort()
44+
ends.sort()
45+
return [bisect.bisect_right(starts, t)-bisect.bisect_right(ends, t) for t in persons]

0 commit comments

Comments
 (0)