Skip to content

Commit 6ac5d90

Browse files
committed
Sync LeetCode submission Runtime - 71 ms (83.57%), Memory - 34.1 MB (28.45%)
1 parent 3e99555 commit 6ac5d90

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>players</code>, where <code>players[i]</code> represents the <strong>ability</strong> of the <code>i<sup>th</sup></code> player. You are also given a <strong>0-indexed</strong> integer array <code>trainers</code>, where <code>trainers[j]</code> represents the <strong>training capacity </strong>of the <code>j<sup>th</sup></code> trainer.</p>
2+
3+
<p>The <code>i<sup>th</sup></code> player can <strong>match</strong> with the <code>j<sup>th</sup></code> trainer if the player&#39;s ability is <strong>less than or equal to</strong> the trainer&#39;s training capacity. Additionally, the <code>i<sup>th</sup></code> player can be matched with at most one trainer, and the <code>j<sup>th</sup></code> trainer can be matched with at most one player.</p>
4+
5+
<p>Return <em>the <strong>maximum</strong> number of matchings between </em><code>players</code><em> and </em><code>trainers</code><em> that satisfy these conditions.</em></p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> players = [4,7,9], trainers = [8,2,5,8]
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong>
14+
One of the ways we can form two matchings is as follows:
15+
- players[0] can be matched with trainers[0] since 4 &lt;= 8.
16+
- players[1] can be matched with trainers[3] since 7 &lt;= 8.
17+
It can be proven that 2 is the maximum number of matchings that can be formed.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> players = [1,1,1], trainers = [10]
24+
<strong>Output:</strong> 1
25+
<strong>Explanation:</strong>
26+
The trainer can be matched with any of the 3 players.
27+
Each player can only be matched with one trainer, so the maximum answer is 1.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= players.length, trainers.length &lt;= 10<sup>5</sup></code></li>
35+
<li><code>1 &lt;= players[i], trainers[j] &lt;= 10<sup>9</sup></code></li>
36+
</ul>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/assign-cookies/description/" target="_blank"> 445: Assign Cookies.</a></p>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Approach: Sorting + Two Pointers + Greedy
2+
3+
# m = len(players), n = len(trainers)
4+
# Time: O(m log m + n log n)
5+
# Space: O(log m + log n)
6+
7+
class Solution:
8+
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
9+
players.sort()
10+
trainers.sort()
11+
m, n = len(players), len(trainers)
12+
i = j = count = 0
13+
14+
while i < m and j < n:
15+
while j < n and players[i] > trainers[j]:
16+
j += 1
17+
if j < n:
18+
count += 1
19+
i += 1
20+
j += 1
21+
22+
return count
23+

0 commit comments

Comments
 (0)