File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ // two heaps
2
+ class Solution {
3
+ fun fullBloomFlowers (flowers : Array <IntArray >, _people : IntArray ): IntArray {
4
+ val people = _people .mapIndexed { i, p -> p to i }.sortedBy { it.first }
5
+ val res = IntArray (_people .size)
6
+ var count = 0
7
+ val start = PriorityQueue <Int >()
8
+ val end = PriorityQueue <Int >()
9
+
10
+ for ((s, e) in flowers) {
11
+ start.add(s)
12
+ end.add(e)
13
+ }
14
+
15
+ for ((p, i) in people) {
16
+ while (start.isNotEmpty() && start.peek() <= p) {
17
+ start.poll()
18
+ count++
19
+ }
20
+
21
+ while (end.isNotEmpty() && end.peek() < p) {
22
+ end.poll()
23
+ count--
24
+ }
25
+
26
+ res[i] = count
27
+ }
28
+
29
+ return res
30
+ }
31
+ }
32
+
33
+ // one heap
34
+ class Solution {
35
+ fun fullBloomFlowers (flowers : Array <IntArray >, _people : IntArray ): IntArray {
36
+ val people = _people .mapIndexed { i, p -> p to i }.sortedBy { it.first }
37
+ val res = IntArray (_people .size)
38
+ flowers.sortBy { it[0 ] }
39
+ val end = PriorityQueue <Int >()
40
+
41
+ var j = 0
42
+ for ((p, i) in people) {
43
+ while (j < flowers.size && flowers[j][0 ] <= p) {
44
+ end.add(flowers[j][1 ])
45
+ j++
46
+ }
47
+
48
+ while (end.isNotEmpty() && end.peek() < p)
49
+ end.poll()
50
+
51
+ res[i] = end.size
52
+ }
53
+
54
+ return res
55
+ }
56
+ }
You can’t perform that action at this time.
0 commit comments