Skip to content

Commit c80fd6a

Browse files
committed
Sync LeetCode submission Runtime - 8 ms (64.55%), Memory - 16.7 MB (23.35%)
1 parent a18d20f commit c80fd6a

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
2+
3+
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed to do this operation <strong>any</strong> number of times (<strong>including zero</strong>).</p>
4+
5+
<p>Return <code>true</code> <em>if you can sort the array, else return </em><code>false</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums = [8,4,2,30,15]
12+
<strong>Output:</strong> true
13+
<strong>Explanation:</strong> Let&#39;s look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation &quot;10&quot;, &quot;100&quot;, and &quot;1000&quot; respectively. The numbers 15 and 30 have four set bits each with binary representation &quot;1111&quot; and &quot;11110&quot;.
14+
We can sort the array using 4 operations:
15+
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
16+
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
17+
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
18+
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].
19+
The array has become sorted, hence we return true.
20+
Note that there may be other sequences of operations which also sort the array.
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,2,3,4,5]
27+
<strong>Output:</strong> true
28+
<strong>Explanation:</strong> The array is already sorted, hence we return true.
29+
</pre>
30+
31+
<p><strong class="example">Example 3:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> nums = [3,16,8,4,2]
35+
<strong>Output:</strong> false
36+
<strong>Explanation:</strong> It can be shown that it is not possible to sort the input array using any number of operations.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
44+
<li><code>1 &lt;= nums[i] &lt;= 2<sup>8</sup></code></li>
45+
</ul>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Approach 2: Sortable Segments
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def canSortArray(self, nums: List[int]) -> bool:
8+
num_of_set_bits = bin(nums[0]).count('1')
9+
max_of_segment = nums[0]
10+
min_of_segment = nums[0]
11+
12+
max_of_prev_segment = float('-inf')
13+
14+
for i in range(len(nums)):
15+
16+
# element belongs to same segment
17+
if bin(nums[i]).count('1') == num_of_set_bits:
18+
# update min and max of segment
19+
max_of_segment = max(max_of_segment, nums[i])
20+
min_of_segment = min(min_of_segment, nums[i])
21+
22+
# element belongs to new segment
23+
else:
24+
# check if segments are arranged properly
25+
if min_of_segment < max_of_prev_segment:
26+
return False
27+
28+
# update max of previous segment
29+
max_of_prev_segment = max_of_segment
30+
31+
# start new segment with current element
32+
max_of_segment = nums[i]
33+
min_of_segment = nums[i]
34+
num_of_set_bits = bin(nums[i]).count('1')
35+
36+
# final check for proper segment arrangement
37+
if min_of_segment < max_of_prev_segment:
38+
return False
39+
40+
return True
41+

0 commit comments

Comments
 (0)