Skip to content

Commit 40f6f81

Browse files
committed
Sync LeetCode submission Runtime - 12599 ms (10.72%), Memory - 36.8 MB (83.48%)
1 parent 59c081d commit 40f6f81

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<p>You are given two arrays of integers, <code>fruits</code> and <code>baskets</code>, each of length <code>n</code>, where <code>fruits[i]</code> represents the <strong>quantity</strong> of the <code>i<sup>th</sup></code> type of fruit, and <code>baskets[j]</code> represents the <strong>capacity</strong> of the <code>j<sup>th</sup></code> basket.</p>
2+
3+
<p>From left to right, place the fruits according to these rules:</p>
4+
5+
<ul>
6+
<li>Each fruit type must be placed in the <strong>leftmost available basket</strong> with a capacity <strong>greater than or equal</strong> to the quantity of that fruit type.</li>
7+
<li>Each basket can hold <b>only one</b> type of fruit.</li>
8+
<li>If a fruit type <b>cannot be placed</b> in any basket, it remains <b>unplaced</b>.</li>
9+
</ul>
10+
11+
<p>Return the number of fruit types that remain unplaced after all possible allocations are made.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">fruits = [4,2,5], baskets = [3,5,4]</span></p>
18+
19+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
20+
21+
<p><strong>Explanation:</strong></p>
22+
23+
<ul>
24+
<li><code>fruits[0] = 4</code> is placed in <code>baskets[1] = 5</code>.</li>
25+
<li><code>fruits[1] = 2</code> is placed in <code>baskets[0] = 3</code>.</li>
26+
<li><code>fruits[2] = 5</code> cannot be placed in <code>baskets[2] = 4</code>.</li>
27+
</ul>
28+
29+
<p>Since one fruit type remains unplaced, we return 1.</p>
30+
</div>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>Input:</strong> <span class="example-io">fruits = [3,6,1], baskets = [6,4,7]</span></p>
36+
37+
<p><strong>Output:</strong> <span class="example-io">0</span></p>
38+
39+
<p><strong>Explanation:</strong></p>
40+
41+
<ul>
42+
<li><code>fruits[0] = 3</code> is placed in <code>baskets[0] = 6</code>.</li>
43+
<li><code>fruits[1] = 6</code> cannot be placed in <code>baskets[1] = 4</code> (insufficient capacity) but can be placed in the next available basket, <code>baskets[2] = 7</code>.</li>
44+
<li><code>fruits[2] = 1</code> is placed in <code>baskets[1] = 4</code>.</li>
45+
</ul>
46+
47+
<p>Since all fruits are successfully placed, we return 0.</p>
48+
</div>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>n == fruits.length == baskets.length</code></li>
55+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
56+
<li><code>1 &lt;= fruits[i], baskets[i] &lt;= 10<sup>9</sup></code></li>
57+
</ul>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import math
2+
3+
class Solution:
4+
def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
5+
n = len(baskets)
6+
m = int(math.sqrt(n))
7+
section = (n + m - 1) // m
8+
count = 0
9+
maxV = [0] * section
10+
11+
for i in range(n):
12+
maxV[i // m] = max(maxV[i // m], baskets[i])
13+
14+
for fruit in fruits:
15+
unset = 1
16+
for sec in range(section):
17+
if maxV[sec] < fruit:
18+
continue
19+
choose = 0
20+
maxV[sec] = 0
21+
for i in range(m):
22+
pos = sec * m + i
23+
if pos < n and baskets[pos] >= fruit and not choose:
24+
baskets[pos] = 0
25+
choose = 1
26+
if pos < n:
27+
maxV[sec] = max(maxV[sec], baskets[pos])
28+
unset = 0
29+
break
30+
count += unset
31+
return count
32+

0 commit comments

Comments
 (0)