Skip to content

Commit f3f6f8f

Browse files
committed
[LeetCode Sync] Runtime - 107 ms (63.02%), Memory - 41.5 MB (65.65%)
1 parent 253b09c commit f3f6f8f

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>flowers</code>, where <code>flowers[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means the <code>i<sup>th</sup></code> flower will be in <strong>full bloom</strong> from <code>start<sub>i</sub></code> to <code>end<sub>i</sub></code> (<strong>inclusive</strong>). You are also given a <strong>0-indexed</strong> integer array <code>people</code> of size <code>n</code>, where <code>people[i]</code> is the time that the <code>i<sup>th</sup></code> person will arrive to see the flowers.</p>
2+
3+
<p>Return <em>an integer array </em><code>answer</code><em> of size </em><code>n</code><em>, where </em><code>answer[i]</code><em> is the <strong>number</strong> of flowers that are in full bloom when the </em><code>i<sup>th</sup></code><em> person arrives.</em></p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/ex1new.jpg" style="width: 550px; height: 216px;" />
8+
<pre>
9+
<strong>Input:</strong> flowers = [[1,6],[3,7],[9,12],[4,13]], people = [2,3,7,11]
10+
<strong>Output:</strong> [1,2,2,2]
11+
<strong>Explanation: </strong>The figure above shows the times when the flowers are in full bloom and when the people arrive.
12+
For each person, we return the number of flowers in full bloom during their arrival.
13+
</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/02/ex2new.jpg" style="width: 450px; height: 195px;" />
17+
<pre>
18+
<strong>Input:</strong> flowers = [[1,10],[3,3]], people = [3,3,2]
19+
<strong>Output:</strong> [2,2,1]
20+
<strong>Explanation:</strong> The figure above shows the times when the flowers are in full bloom and when the people arrive.
21+
For each person, we return the number of flowers in full bloom during their arrival.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= flowers.length &lt;= 5 * 10<sup>4</sup></code></li>
29+
<li><code>flowers[i].length == 2</code></li>
30+
<li><code>1 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
31+
<li><code>1 &lt;= people.length &lt;= 5 * 10<sup>4</sup></code></li>
32+
<li><code>1 &lt;= people[i] &lt;= 10<sup>9</sup></code></li>
33+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
3+
dic = defaultdict(int)
4+
for start, end in flowers:
5+
dic[start] += 1
6+
dic[end + 1] -= 1
7+
8+
m = len(people)
9+
set_nums = sorted(dic)
10+
result = [0] * m
11+
sum_val = 0
12+
i = 0
13+
14+
for t, j in sorted(zip(people, range(m))):
15+
while i < len(set_nums) and set_nums[i] <= t:
16+
sum_val += dic[set_nums[i]]
17+
i += 1
18+
result[j] = sum_val
19+
20+
return result

0 commit comments

Comments
 (0)