Skip to content

Commit 735d3c7

Browse files
Create 2251-number-of-flowers-in-full-bloom.java
1 parent 100249a commit 735d3c7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int[] fullBloomFlowers(int[][] flowers, int[] people) {
3+
List<Integer> starts = new ArrayList<>();
4+
List<Integer> ends = new ArrayList<>();
5+
6+
for(int[] flwr: flowers){
7+
starts.add(flwr[0]);
8+
ends.add(flwr[1] + 1);
9+
}
10+
11+
Collections.sort(starts);
12+
Collections.sort(ends);
13+
int[] res = new int[people.length];
14+
15+
for(int i = 0; i < people.length; i++){
16+
int person = people[i];
17+
int startBlooming = BinarySearch(starts, person);
18+
int stopBlooming = BinarySearch(ends, person);
19+
res[i] = startBlooming - stopBlooming;
20+
}
21+
return res;
22+
}
23+
24+
private int BinarySearch(List<Integer> ls, int target){
25+
int l = 0;
26+
int r = ls.size();
27+
28+
while(l < r){
29+
int m = l + (r-l)/2;
30+
if(target < ls.get(m))
31+
r = m;
32+
else
33+
l = m + 1;
34+
}
35+
return l;
36+
}
37+
}

0 commit comments

Comments
 (0)