Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit100249a

Browse files
authored
Create 2251-number-of-flowers-in-full-bloom.kt
1 parenta20a0e6 commit100249a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// two heaps
2+
classSolution {
3+
funfullBloomFlowers(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+
classSolution {
35+
funfullBloomFlowers(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+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp