Skip to content

Commit b0c50a3

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.8 MB (73.03%)
1 parent 4ff14c6 commit b0c50a3

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>You are given an integer array <code>nums</code> consisting of <code>2 * n</code> integers.</p>
2+
3+
<p>You need to divide <code>nums</code> into <code>n</code> pairs such that:</p>
4+
5+
<ul>
6+
<li>Each element belongs to <strong>exactly one</strong> pair.</li>
7+
<li>The elements present in a pair are <strong>equal</strong>.</li>
8+
</ul>
9+
10+
<p>Return <code>true</code> <em>if nums can be divided into</em> <code>n</code> <em>pairs, otherwise return</em> <code>false</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> nums = [3,2,3,2,2,2]
17+
<strong>Output:</strong> true
18+
<strong>Explanation:</strong>
19+
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
20+
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,2,3,4]
27+
<strong>Output:</strong> false
28+
<strong>Explanation:</strong>
29+
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>nums.length == 2 * n</code></li>
37+
<li><code>1 &lt;= n &lt;= 500</code></li>
38+
<li><code>1 &lt;= nums[i] &lt;= 500</code></li>
39+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Approach 2: Map
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
from collections import Counter
7+
8+
class Solution:
9+
def divideArray(self, nums: List[int]) -> bool:
10+
freq = Counter(nums)
11+
12+
return all(count % 2 == 0 for count in freq.values())
13+

0 commit comments

Comments
 (0)