File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments