LeetCode 2251. Number of flowers during the flowering period: sort + binary

【LetMeFly】2251. Number of flowers during the flowering period: sort + two points

Leetcode question link: https://leetcode.cn/problems/number-of-flowers-in-full-bloom/

You are given a two-dimensional integer array with subscripts  starting  from 0flowers  , which   represents  the flowering period  of the th flower  from   to   (all inclusive ). At the same time, you are given an  integer array with  subscripts starting from 0  and a size of 0  ,  which is   the time spent by the first person.flowers[i] = [starti, endi]istartiendinpersonspersons[i]i

Please return an n integer array of size  , where  is the number  of flowers during the flowering period when  the first  person arrives  . answeranswer[i]i

 

Example 1:

Input: flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
 Output: [1,2,2, 2]
 Explanation: The picture above shows the flowering time of each flower and the arrival time of each person. 
For each individual, we return the number of flowers in the flowering period when they arrived.

Example 2:

Input: flowers = [[1,10],[3,3]], persons = [3,3,2]
 Output: [2,2,1]
 Explanation: The above figure shows the flowering time of each flower, and Arrival time for everyone. 
For each individual, we return the number of flowers in the flowering period when they arrived.

 

hint:

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

Method 1: Sorting + Dividing

Put all the flowering times into an array and sort them from small to large; put all the closed flowering times into an array and sort them from small to large.

For a certain moment (a certain day), the number of flowers currently blooming is: the number of flowers with a blooming time less than or equal to the current time - the number of closed flowers less than or equal to the current time the number of flowers with a blooming time less than or equal to the current time - the number of closed flowers The number of flowers less than or equal to the day before the current timeThe number of flowers whose flowering time is less than or equal to the current timeThe number of closed flowers is less than or equal to the number of flowers one day before the current time .

How to quickly get non-descending array aaa in≤ k \leq kThe number of elements of k ? Just two points. (C++’s upper_bound / Python’s bisect_right)

  • Time complexity O ( ( n + m ) log ⁡ n ) O((n + m)\log n)O (( n+m)logn ) ,exceptn= len ( flowers ) n = len ( flowers )n=len(flowers) m = l e n ( p e o p l e ) m = len(people) m=len(people)
  • Space complexity O ( n ) O(n)O ( n ) , the return value is not included in the algorithm space complexity

AC code

C++
class Solution {
    
    
public:
    vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) {
    
    
        vector<int> start(flowers.size()), end(flowers.size()), ans(people.size());
        for (int i = 0; i < flowers.size(); i++) {
    
    
            start[i] = flowers[i][0];
            end[i] = flowers[i][1];
        }
        sort(start.begin(), start.end());
        sort(end.begin(), end.end());

        for (int i = 0; i < people.size(); i++) {
    
    
            // 到这一天为止的开花总数 - 到这一天的前一天为止的闭花总数
            int hanagasaku = upper_bound(start.begin(), start.end(), people[i]) - start.begin();  // 花が咲く(はながさく)
            int hanagatiru = upper_bound(end.begin(), end.end(), people[i] - 1) - end.begin();//  花が散る(はながちる)
            ans[i] = hanagasaku - hanagatiru;
        }
        return ans;
    }
};
Python

True and simple!

# from typing import List
# from bisect import bisect_right

class Solution:
    def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
        start = sorted([f[0] for f in flowers])
        end = sorted([f[1] for f in flowers])
        return [bisect_right(start, p) - bisect_right(end, p - 1) for p in people]

The article is published simultaneously on CSDN. It is not easy to be original. Please attach the link to the original article after reprinting with the author's consent ~
Tisfy: https://letmefly.blog.csdn.net/article/details/133378624

Guess you like

Origin blog.csdn.net/Tisfy/article/details/133378624