Loading...

10-11 leetcode 2251

链接 2251. Number of Flowers in Full Bloom

题目

You are given a 0-indexed 2D integer array flowers, where flowers[i] = [starti, endi] means the ith flower will be in full bloom from starti to endi (inclusive). You are also given a 0-indexed integer array people of size n, where people[i] is the time that the ith person will arrive to see the flowers.

Return an integer array answer of size n, where answer[i] is the number of flowers that are in full bloom when the ith person arrives.

题解

这题还是一个二分,把开始时间和结束时间排序,然后二分查找

1
2
3
4
5
6
7
8
9
10
from bisect import bisect_left, bisect_right


class Solution:
def fullBloomFlowers(
self, flowers: list[list[int]], people: list[int]
) -> list[int]:
start = sorted(s for s, e in flowers)
end = sorted(e for s, e in flowers)
return [bisect_right(start, p) - bisect_left(end, p) for p in people]

Comment