Skip to content

Commit a9d1f87

Browse files
committed
Sync LeetCode submission Runtime - 155 ms (19.74%), Memory - 43.1 MB (77.33%)
1 parent 51ce92f commit a9d1f87

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

2689-rearranging-fruits/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>You have two fruit baskets containing <code>n</code> fruits each. You are given two <strong>0-indexed</strong> integer arrays <code>basket1</code> and <code>basket2</code> representing the cost of fruit in each basket. You want to make both baskets <strong>equal</strong>. To do so, you can use the following operation as many times as you want:</p>
2+
3+
<ul>
4+
<li>Choose two indices <code>i</code> and <code>j</code>, and swap the <code>i<sup><font size="1">th</font></sup></code> fruit of <code>basket1</code> with the <code>j<sup><font size="1">th</font></sup></code> fruit of <code>basket2</code>.</li>
5+
<li>The cost of the swap is <code>min(basket1[i], basket2[j])</code>.</li>
6+
</ul>
7+
8+
<p>Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.</p>
9+
10+
<p>Return <em>the minimum cost to make both the baskets equal or </em><code>-1</code><em> if impossible.</em></p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> basket1 = [4,2,2,2], basket2 = [1,4,1,2]
17+
<strong>Output:</strong> 1
18+
<strong>Explanation:</strong> Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> basket1 = [2,3,4,1], basket2 = [3,2,5,1]
25+
<strong>Output:</strong> -1
26+
<strong>Explanation:</strong> It can be shown that it is impossible to make both the baskets equal.
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>basket1.length == basket2.length</code></li>
34+
<li><code>1 &lt;= basket1.length &lt;= 10<sup>5</sup></code></li>
35+
<li><code>1 &lt;= basket1[i], basket2[i] &lt;= 10<sup>9</sup></code></li>
36+
</ul>

2689-rearranging-fruits/solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Approach: Greedy
2+
3+
# Time: O(n log n)
4+
# Space: O(n)
5+
6+
from collections import Counter
7+
8+
class Solution:
9+
def minCost(self, basket1: List[int], basket2: List[int]) -> int:
10+
freq = Counter()
11+
m = float('inf')
12+
13+
for b1 in basket1:
14+
freq[b1] += 1
15+
m = min(m, b1)
16+
for b2 in basket2:
17+
freq[b2] -= 1
18+
m = min(m, b2)
19+
20+
merge = []
21+
for k, c in freq.items():
22+
if c % 2 != 0:
23+
return -1
24+
merge.extend([k] * (abs(c) // 2))
25+
26+
if not merge:
27+
return 0
28+
merge.sort()
29+
return sum(min(2 * m, x) for x in merge[: len(merge) // 2])
30+

0 commit comments

Comments
 (0)