LeetCode每日一题(2251. Number of Flowers in Full Bloom)

给定一个二维数组flowers,表示花朵盛开的时间段,和一个整数数组persons,表示人们参观的时间。返回一个数组,其中answer[i]是第i个人到达时盛开的花朵数量。通过按开花时间排序并使用最小堆来维护当前盛开的花朵,计算每个人到达时的花朵数量。

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 persons of size n, where persons[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.

Example 1:

Input: flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
Output: [1,2,2,2]

Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.

Example 2:

Input: flowers = [[1,10],[3,3]], persons = [3,3,2]
Output: [2,2,1]

Explanation: The figure above shows the times when the flowers are in full bloom and when the people arrive.
For each person, we return the number of flowers in full bloom during their arrival.

Constraints:

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

  1. 将 flowers 按开始时间(start)进行排序, 如果开始时间(start)相同则按结束时间(end)排序
  2. 用一个 min heap 来维护当前开放的花, heap 中保存的是花的凋谢时间
  3. 按时间顺序对 persons 进行排序, 使得 persons[i] = (access_time, index)
  4. 遍历 persons, 每次遍历将 flowers 中 start <= access_time 的 flower 放到 heap 中,如果遇到 start > access_time 的情况则终止向 heap 中添加 flower 。然后再将 heap 中 end < access_time 的 flower 移出 heap, 此时 heap 中剩余的就是该 person 访问时盛开的 flowers。


use std::cmp::Reverse;
use std::collections::BinaryHeap;

impl Solution {
    pub fn full_bloom_flowers(flowers: Vec<Vec<i32>>, persons: Vec<i32>) -> Vec<i32> {
        let mut flowers: Vec<(i32, i32)> = flowers.into_iter().map(|l| (l[0], l[1])).collect();
        flowers.sort();
        flowers.reverse();
        let mut persons: Vec<(i32, usize)> = persons
            .into_iter()
            .enumerate()
            .map(|(i, v)| (v, i))
            .collect();
        persons.sort();
        let mut ans = vec![0; persons.len()];
        let mut bloomings: BinaryHeap<Reverse<i32>> = BinaryHeap::new();
        for (t, i) in persons {
            while let Some((start, end)) = flowers.pop() {
                if start > t {
                    flowers.push((start, end));
                    break;
                }
                bloomings.push(Reverse(end));
            }
            while let Some(Reverse(end)) = bloomings.pop() {
                if end >= t {
                    bloomings.push(Reverse(end));
                    break;
                }
            }
            ans[i] = bloomings.len() as i32;
        }
        ans
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值