给你一个下标从 0 开始的二维整数数组 flowers ,其中 flowers[i] = [starti, endi] 表示第 i 朵花的 花期 从 starti 到 endi (都 包含)。同时给你一个下标从 0 开始大小为 n 的整数数组 persons ,persons[i] 是第 i 个人来看花的时间。
请你返回一个大小为 n 的整数数组 **answer ,其中 answer[i]是第 i 个人到达时在花期内花的 数目 。
示例 1:
输入: flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
输出: [1,2,2,2]
解释: 上图展示了每朵花的花期时间,和每个人的到达时间。
对每个人,我们返回他们到达时在花期内花的数目。
示例 2:
输入: flowers = [[1,10],[3,3]], persons = [3,3,2]
输出: [2,2,1]
解释: 上图展示了每朵花的花期时间,和每个人的到达时间。
对每个人,我们返回他们到达时在花期内花的数目。
提示:
1 <= flowers.length <= 5 * 10^4flowers[i].length == 21 <= starti <= endi <= 10^91 <= persons.length <= 5 * 10^41 <= persons[i] <= 10^9
解析: 真难为这5万人,耗时如此之久去看花了。。。。 简单的做法就是直接计数,用一个字典,key是到达时间,value是int,有一朵话就加1,便利所有花的花期,给这个超大字典准备好就行了。很符合预期的是,超时了。。。 好吧,还是使用SortedList,感觉今天的题目是在教我如何使用SortedList。真的会谢!
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
bloom_sorted_list = SortedList()
down_sorted_list = SortedList()
for start, end in flowers:
bloom_sorted_list.add(start)
down_sorted_list.add(end)
count_list = []
for d in persons:
count = bloom_sorted_list.bisect_right(d) - down_sorted_list.bisect_left(d)
count_list.append(count)
return count_list