{"id":9692,"date":"2022-04-27T22:10:51","date_gmt":"2022-04-28T05:10:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9692"},"modified":"2022-04-28T21:26:51","modified_gmt":"2022-04-29T04:26:51","slug":"leetcode-2251-number-of-flowers-in-full-bloom","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-2251-number-of-flowers-in-full-bloom\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2251. Number of Flowers in Full Bloom"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>flowers<\/code>, where&nbsp;<code>flowers[i] = [start<sub>i<\/sub>, end<sub>i<\/sub>]<\/code>&nbsp;means the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;flower will be in&nbsp;<strong>full bloom<\/strong>&nbsp;from&nbsp;<code>start<sub>i<\/sub><\/code>&nbsp;to&nbsp;<code>end<sub>i<\/sub><\/code>&nbsp;(<strong>inclusive<\/strong>). You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>persons<\/code>&nbsp;of size&nbsp;<code>n<\/code>, where&nbsp;<code>persons[i]<\/code>&nbsp;is the time that the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;person will arrive to see the flowers.<\/p>\n\n\n\n<p>Return&nbsp;<em>an integer array&nbsp;<\/em><code>answer<\/code><em>&nbsp;of size&nbsp;<\/em><code>n<\/code><em>, where&nbsp;<\/em><code>answer[i]<\/code><em>&nbsp;is the&nbsp;<strong>number<\/strong>&nbsp;of flowers that are in full bloom when the&nbsp;<\/em><code>i<sup>th<\/sup><\/code><em>&nbsp;person arrives.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/03\/02\/ex1new.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]\n<strong>Output:<\/strong> [1,2,2,2]\n<strong>Explanation: <\/strong>The figure above shows the times when the flowers are in full bloom and when the people arrive.\nFor each person, we return the number of flowers in full bloom during their arrival.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/03\/02\/ex2new.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> flowers = [[1,10],[3,3]], persons = [3,3,2]\n<strong>Output:<\/strong> [2,2,1]\n<strong>Explanation:<\/strong> The figure above shows the times when the flowers are in full bloom and when the people arrive.\nFor each person, we return the number of flowers in full bloom during their arrival.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= flowers.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>flowers[i].length == 2<\/code><\/li><li><code>1 &lt;= start<sub>i<\/sub>&nbsp;&lt;= end<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= persons.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= persons[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum + Binary Search<\/strong><\/h2>\n\n\n\n<p>Use a treemap to store the counts (ordered by time t), when a flower begins to bloom at start, we increase m[start], when it dies at end, we decrease m[end+1]. prefix_sum[t] indicates the # of blooming flowers at time t.<\/p>\n\n\n\n<p>For each people, use binary search to find the latest # of flowers before his arrival.<\/p>\n\n\n\n<p>Time complexity: O(nlogn + mlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& persons) {\n    map<int, int> m{{0, 0}};\n    for (const auto& f : flowers)\n      ++m[f[0]], --m[f[1] + 1];    \n    int sum = 0;\n    for (auto& [t, c] : m)\n      c = sum += c;    \n    vector<int> ans;    \n    for (int t : persons)      \n      ans.push_back(prev(m.upper_bound(t))->second);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;2D integer array&nbsp;flowers, where&nbsp;flowers[i] = [starti, endi]&nbsp;means the&nbsp;ith&nbsp;flower will be in&nbsp;full bloom&nbsp;from&nbsp;starti&nbsp;to&nbsp;endi&nbsp;(inclusive). You are also given a&nbsp;0-indexed&nbsp;integer array&nbsp;persons&nbsp;of size&nbsp;n, where&nbsp;persons[i]&nbsp;is the time&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[217,104,699],"class_list":["post-9692","post","type-post","status-publish","format-standard","hentry","category-tree","tag-hard","tag-sweep-line","tag-treemap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9692"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9692\/revisions"}],"predecessor-version":[{"id":9702,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9692\/revisions\/9702"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}