Skip to content

Commit 8bd56f8

Browse files
authored
2025-01-15 v. 8.0.7: added "2429. Minimize XOR"
2 parents 89a1f63 + dbfce66 commit 8bd56f8

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
646646
| 946. Validate Stack Sequences | [Link](https://leetcode.com/problems/validate-stack-sequences/) | [Link](./lib/medium/946_validate_stack_sequences.rb) | [Link](./test/medium/test_946_validate_stack_sequences.rb) |
647647
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
648648
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
649+
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
649650
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |
650651
| 3223. Minimum Length of String After Operations | [Link](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [Link](./lib/medium/3223_minimum_length_of_string_after_operations.rb) | [Link](./test/medium/test_3223_minimum_length_of_string_after_operations.rb) |
651652

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.0.6'
8+
s.version = '8.0.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/2429_minimize_xor.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/minimize-xor/
4+
# @param {Integer} num1
5+
# @param {Integer} num2
6+
# @return {Integer}
7+
def minimize_xor(num1, num2)
8+
a = num1.to_s(2).count('1')
9+
b = num2.to_s(2).count('1')
10+
11+
result = num1
12+
13+
(0...32).each do |i|
14+
if a > b && (1 << i) & num1 != 0
15+
result ^= 1 << i
16+
a -= 1
17+
end
18+
19+
if a < b && ((1 << i) & num1).zero?
20+
result ^= 1 << i
21+
a += 1
22+
end
23+
end
24+
25+
result
26+
end

test/medium/test_2429_minimize_xor.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/2429_minimize_xor'
5+
require 'minitest/autorun'
6+
7+
class MinimizeXORTest < ::Minitest::Test
8+
def test_default_one = assert_equal(3, minimize_xor(3, 5))
9+
10+
def test_default_two = assert_equal(3, minimize_xor(1, 12))
11+
12+
def test_additional_one = assert_equal(24, minimize_xor(25, 72))
13+
end

0 commit comments

Comments
 (0)