LeetCode 题解工作台

花期内花的数目

给你一个下标从 0 开始的二维整数数组 flowers ,其中 flowers[i] = [start i , end i ] 表示第 i 朵花的 花期 从 start i 到 end i (都 包含 )。同时给你一个下标从 0 开始大小为 n 的整数数组 people , people[i] 是第…

category

6

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

困难 · 数组·哈希·扫描

bolt

答案摘要

我们将花按照开始时间和结束时间分别排序,然后对于每个人,我们可以使用二分查找来找到他们到达时在花期内花的数目。就是说,找出在每个人到达时,已经开花的花的数目,减去在每个人到达时,已经凋谢的花的数目,即可得到答案。 时间复杂度 $O((m + n) \times \log n)$,空间复杂度 。其中 和 分别是数组 和 的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的二维整数数组 flowers ,其中 flowers[i] = [starti, endi] 表示第 i 朵花的 花期 从 starti 到 endi (都 包含)。同时给你一个下标从 0 开始大小为 n 的整数数组 peoplepeople[i] 是第 i 个人来看花的时间。

请你返回一个大小为 n 的整数数组 answer ,其中 answer[i]是第 i 个人到达时在花期内花的 数目 。

 

示例 1:

输入:flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11]
输出:[1,2,2,2]
解释:上图展示了每朵花的花期时间,和每个人的到达时间。
对每个人,我们返回他们到达时在花期内花的数目。

示例 2:

输入:flowers = [[1,10],[3,3]], people = [3,3,2]
输出:[2,2,1]
解释:上图展示了每朵花的花期时间,和每个人的到达时间。
对每个人,我们返回他们到达时在花期内花的数目。

 

提示:

  • 1 <= flowers.length <= 5 * 104
  • flowers[i].length == 2
  • 1 <= starti <= endi <= 109
  • 1 <= people.length <= 5 * 104
  • 1 <= people[i] <= 109
lightbulb

解题思路

方法一:排序 + 二分查找

我们将花按照开始时间和结束时间分别排序,然后对于每个人,我们可以使用二分查找来找到他们到达时在花期内花的数目。就是说,找出在每个人到达时,已经开花的花的数目,减去在每个人到达时,已经凋谢的花的数目,即可得到答案。

时间复杂度 O((m+n)×logn)O((m + n) \times \log n),空间复杂度 O(n)O(n)。其中 nnmm 分别是数组 flowers\textit{flowers}people\textit{people} 的长度。

1
2
3
4
5
6
7
class Solution:
    def fullBloomFlowers(
        self, flowers: List[List[int]], people: List[int]
    ) -> List[int]:
        start, end = sorted(a for a, _ in flowers), sorted(b for _, b in flowers)
        return [bisect_right(start, p) - bisect_left(end, p) for p in people]
speed

复杂度分析

指标
时间O((n + m) \cdot \log{n})
空间O(n)
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for familiarity with binary search and sorting techniques.

  • question_mark

    Assess the candidate's ability to handle large input sizes with an optimized approach.

  • question_mark

    Evaluate whether the candidate can effectively apply hash maps and binary search together.

warning

常见陷阱

外企场景
  • error

    Forgetting to sort the flower intervals and people's arrival times, which will lead to inefficient solutions.

  • error

    Not utilizing binary search or hash maps, which can result in a slower, less optimized solution.

  • error

    Misunderstanding the problem by assuming all flowers are blooming at every time point, leading to incorrect results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider adding extra constraints, such as limiting the number of flowers or the range of bloom times.

  • arrow_right_alt

    Explore how the solution changes if the flowers' blooming times are not continuous, adding complexity to the bloom intervals.

  • arrow_right_alt

    Alter the problem to calculate the number of flowers in bloom over a specific range of times instead of for individual people.

help

常见问题

外企场景

花期内花的数目题解:数组·哈希·扫描 | LeetCode #2251 困难