Skip to content

Commit 912415f

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (18.40%), Memory - 17.7 MB (41.60%)
1 parent 3d2c595 commit 912415f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

2509-minimize-xor/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>Given two positive integers <code>num1</code> and <code>num2</code>, find the positive integer <code>x</code> such that:</p>
2+
3+
<ul>
4+
<li><code>x</code> has the same number of set bits as <code>num2</code>, and</li>
5+
<li>The value <code>x XOR num1</code> is <strong>minimal</strong>.</li>
6+
</ul>
7+
8+
<p>Note that <code>XOR</code> is the bitwise XOR operation.</p>
9+
10+
<p>Return <em>the integer </em><code>x</code>. The test cases are generated such that <code>x</code> is <strong>uniquely determined</strong>.</p>
11+
12+
<p>The number of <strong>set bits</strong> of an integer is the number of <code>1</code>&#39;s in its binary representation.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> num1 = 3, num2 = 5
19+
<strong>Output:</strong> 3
20+
<strong>Explanation:</strong>
21+
The binary representations of num1 and num2 are 0011 and 0101, respectively.
22+
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 3 = 0</code> is minimal.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> num1 = 1, num2 = 12
29+
<strong>Output:</strong> 3
30+
<strong>Explanation:</strong>
31+
The binary representations of num1 and num2 are 0001 and 1100, respectively.
32+
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 1 = 2</code> is minimal.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>1 &lt;= num1, num2 &lt;= 10<sup>9</sup></code></li>
40+
</ul>

2509-minimize-xor/solution.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Approach 2: Building the Answer
2+
3+
# Time: O(log n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def minimizeXor(self, num1: int, num2: int) -> int:
8+
result = 0
9+
10+
target_set_bits_count = bin(num2).count('1')
11+
set_bits_count = 0
12+
current_bit = 31 # Start from most significant bit
13+
14+
while set_bits_count < target_set_bits_count:
15+
# If the current bit of num1 is set or we must set all remaining bits in result
16+
if self._is_set(num1, current_bit) or (target_set_bits_count - set_bits_count > current_bit):
17+
result = self._set_bit(result, current_bit)
18+
set_bits_count += 1
19+
current_bit -= 1
20+
21+
return result
22+
23+
24+
# Helper function to check if the given bit position in result is set (1).
25+
def _is_set(self, x, bit):
26+
return (x & (1 << bit)) != 0
27+
28+
# Helper function to set the given bit position in result to 1.
29+
def _set_bit(self, x, bit):
30+
return x | (1 << bit)
31+

0 commit comments

Comments
 (0)