Skip to content

Commit 875327c

Browse files
committed
Runtime: 394 ms (Top 78.26%) | Memory: 80.8 MB (Top 73.91%)
1 parent 27477fa commit 875327c

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 394 ms (Top 78.26%) | Memory: 80.8 MB (Top 73.91%)
12
/**
23
* @param {number[][]} flowers
34
* @param {number[]} persons
@@ -6,55 +7,55 @@
67
var fullBloomFlowers = function(flowers, persons) {
78
// *** IDEATION *** //
89
// key observation:
9-
// total number of flowers see on day i =
10+
// total number of flowers see on day i =
1011
// number of flowers has already bloomed so far on day i - number of flowers already ended ("died") prior to day i
11-
12+
1213
// find number of flowers already bloomed on day i
1314
// each flower has a start day
1415
// 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:
1617
// equivalent to find the number of elements in the "start" array which is less than or equal to 8
17-
18+
1819
// find number of flowers already ended on day i
1920
// each flower has an end day
2021
// end array = [6, 7, 12, 13] for example
2122
// on day 8, there are 2 flowers already ended:
2223
// equivalent to find the number of elements in the "end" array which is less than 8
2324
// equivalent to find the number of elements in the "end" array which is less than or equal to 7
24-
25+
2526
// both process above can be solved efficiently with binary search on a sorted array
2627
// hence we need to first build 2 array "start" and "end" and sorted it
2728
// then apply the formula at the beginning to return the required answer
28-
29+
2930
// *** IMPLEMENTATION *** //
3031
// step 1: build the "start" and "end" array
3132
let start = [];
3233
let end = [];
33-
34+
3435
for (let i = 0; i < flowers.length; i++) {
3536
start[i] = flowers[i][0];
3637
end[i] = flowers[i][1];
3738
}
38-
39+
3940
// step 2: sort the "start" and "end" array
4041
start.sort((a, b) => a - b);
4142
end.sort((a, b) => a - b);
42-
43+
4344
// step 3: apply the observation formula using a binarySearch function
4445
let res = [];
4546
for (let j = 0; j < persons.length; j++) {
4647
res[j] = binarySearch(start, persons[j]) - binarySearch(end, persons[j] - 1);
4748
}
4849
return res;
49-
50+
5051
// step 4: implement the binarySearch function (variant from standard binarySearch)
5152
function binarySearch(array, k) {
52-
// array is sorted;
53+
// array is sorted;
5354
// obj is to find the number of elements in array which is less than or equal to "k"
5455
let left = 0;
5556
let right = array.length - 1;
5657
let index = -1;
57-
58+
5859
while (left <= right) {
5960
let mid = left + Math.floor((right - left) / 2);
6061
if (k < array[mid]) {
@@ -64,8 +65,8 @@ var fullBloomFlowers = function(flowers, persons) {
6465
left = mid + 1;
6566
}
6667
}
67-
68+
6869
// 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;
7071
}
71-
};
72+
};

0 commit comments

Comments
 (0)