forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-flowers-in-full-bloom.py
45 lines (38 loc) · 1.1 KB
/
number-of-flowers-in-full-bloom.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
# Time: O(nlogn + mlogn)
# Space: O(n)
import bisect
# line sweep, binary search
class Solution(object):
def fullBloomFlowers(self, flowers, persons):
"""
:type flowers: List[List[int]]
:type persons: List[int]
:rtype: List[int]
"""
cnt = collections.Counter()
for s, e in flowers:
cnt[s] += 1
cnt[e+1] -= 1
events = sorted(cnt.iterkeys())
prefix = [0]
for x in events:
prefix.append(prefix[-1]+cnt[x])
return [prefix[bisect.bisect_right(events, t)] for t in persons]
# Time: O(nlogn + mlogn)
# Space: O(n)
import bisect
# binary search
class Solution(object):
def fullBloomFlowers(self, flowers, persons):
"""
:type flowers: List[List[int]]
:type persons: List[int]
:rtype: List[int]
"""
starts, ends = [], []
for s, e in flowers:
starts.append(s)
ends.append(e+1)
starts.sort()
ends.sort()
return [bisect.bisect_right(starts, t)-bisect.bisect_right(ends, t) for t in persons]