File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] fullBloomFlowers (int [][] flowers , int [] people ) {
3
+ //separate the start and end times and sort them
4
+ int [] start = new int [flowers .length ];
5
+ int [] end = new int [flowers .length ];
6
+
7
+ for (int i = 0 ; i <flowers .length ; i ++) {
8
+ start [i ] = flowers [i ][0 ];
9
+ end [i ] = flowers [i ][1 ];
10
+ }
11
+ Arrays .sort (start );
12
+ Arrays .sort (end );
13
+
14
+ int [] result = new int [people .length ];
15
+
16
+ //use binary search and find the number of flowers blooming
17
+ for (int i = 0 ; i <people .length ; i ++) {
18
+ int bloom = blooming (start , people [i ]);
19
+ int notBloom = notBlooming (end , people [i ]);
20
+ result [i ] = bloom - notBloom ;
21
+ }
22
+
23
+ return result ;
24
+ }
25
+
26
+ //find upper bound of flowers blooming
27
+ public int blooming (int [] start , int index ) {
28
+ int left = 0 ;
29
+ int right = start .length ;
30
+
31
+ while (left <right ) {
32
+ int mid = left + (right -left )/2 ;
33
+ if (start [mid ] <= index ) {
34
+ left = mid + 1 ;
35
+ } else {
36
+ right = mid ;
37
+ }
38
+ }
39
+ return right ;
40
+ }
41
+
42
+ //lower bound of flowers not blooming
43
+ public int notBlooming (int [] start , int index ) {
44
+ int left = 0 ;
45
+ int right = start .length ;
46
+
47
+ while (left <right ) {
48
+ int mid = left + (right -left )/2 ;
49
+ if (start [mid ] < index ) {
50
+ left = mid + 1 ;
51
+ } else {
52
+ right = mid ;
53
+ }
54
+ }
55
+ return right ;
56
+ }
57
+ }
You can’t perform that action at this time.
0 commit comments