Skip to content

Commit cecb540

Browse files
authored
Create Number of Flowers in Full Bloom.java
1 parent 5695ae7 commit cecb540

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int[] fullBloomFlowers(int[][] flowers, int[] people) {
3+
int[] peopleCopy = Arrays.copyOf(people, people.length);
4+
Arrays.sort(peopleCopy);
5+
Arrays.sort(flowers, (a, b) -> Arrays.compare(a, b));
6+
Map<Integer, Integer> map = new HashMap<>();
7+
PriorityQueue<Integer> pq = new PriorityQueue<>();
8+
int idx = 0;
9+
for (int person : peopleCopy) {
10+
while (idx < flowers.length && flowers[idx][0] <= person) {
11+
pq.add(flowers[idx][1]);
12+
idx++;
13+
}
14+
while (!pq.isEmpty() && pq.peek() < person) {
15+
pq.poll();
16+
}
17+
map.put(person, pq.size());
18+
}
19+
int[] result = new int[people.length];
20+
for (int i = 0; i < people.length; i++) {
21+
result[i] = map.get(people[i]);
22+
}
23+
return result;
24+
}
25+
}

0 commit comments

Comments
 (0)