1
+ // Runtime: 394 ms (Top 78.26%) | Memory: 80.8 MB (Top 73.91%)
1
2
/**
2
3
* @param {number[][] } flowers
3
4
* @param {number[] } persons
6
7
var fullBloomFlowers = function ( flowers , persons ) {
7
8
// *** IDEATION *** //
8
9
// key observation:
9
- // total number of flowers see on day i =
10
+ // total number of flowers see on day i =
10
11
// number of flowers has already bloomed so far on day i - number of flowers already ended ("died") prior to day i
11
-
12
+
12
13
// find number of flowers already bloomed on day i
13
14
// each flower has a start day
14
15
// start array = [1, 3, 9, 4] for example
15
- // on day 8, there are 3 flowers bloomed:
16
+ // on day 8, there are 3 flowers bloomed:
16
17
// equivalent to find the number of elements in the "start" array which is less than or equal to 8
17
-
18
+
18
19
// find number of flowers already ended on day i
19
20
// each flower has an end day
20
21
// end array = [6, 7, 12, 13] for example
21
22
// on day 8, there are 2 flowers already ended:
22
23
// equivalent to find the number of elements in the "end" array which is less than 8
23
24
// equivalent to find the number of elements in the "end" array which is less than or equal to 7
24
-
25
+
25
26
// both process above can be solved efficiently with binary search on a sorted array
26
27
// hence we need to first build 2 array "start" and "end" and sorted it
27
28
// then apply the formula at the beginning to return the required answer
28
-
29
+
29
30
// *** IMPLEMENTATION *** //
30
31
// step 1: build the "start" and "end" array
31
32
let start = [ ] ;
32
33
let end = [ ] ;
33
-
34
+
34
35
for ( let i = 0 ; i < flowers . length ; i ++ ) {
35
36
start [ i ] = flowers [ i ] [ 0 ] ;
36
37
end [ i ] = flowers [ i ] [ 1 ] ;
37
38
}
38
-
39
+
39
40
// step 2: sort the "start" and "end" array
40
41
start . sort ( ( a , b ) => a - b ) ;
41
42
end . sort ( ( a , b ) => a - b ) ;
42
-
43
+
43
44
// step 3: apply the observation formula using a binarySearch function
44
45
let res = [ ] ;
45
46
for ( let j = 0 ; j < persons . length ; j ++ ) {
46
47
res [ j ] = binarySearch ( start , persons [ j ] ) - binarySearch ( end , persons [ j ] - 1 ) ;
47
48
}
48
49
return res ;
49
-
50
+
50
51
// step 4: implement the binarySearch function (variant from standard binarySearch)
51
52
function binarySearch ( array , k ) {
52
- // array is sorted;
53
+ // array is sorted;
53
54
// obj is to find the number of elements in array which is less than or equal to "k"
54
55
let left = 0 ;
55
56
let right = array . length - 1 ;
56
57
let index = - 1 ;
57
-
58
+
58
59
while ( left <= right ) {
59
60
let mid = left + Math . floor ( ( right - left ) / 2 ) ;
60
61
if ( k < array [ mid ] ) {
@@ -64,8 +65,8 @@ var fullBloomFlowers = function(flowers, persons) {
64
65
left = mid + 1 ;
65
66
}
66
67
}
67
-
68
+
68
69
// all elements with in array from position '0' to position 'index' will be less than or equal to k
69
- return index + 1 ;
70
+ return index + 1 ;
70
71
}
71
- } ;
72
+ } ;
0 commit comments