diff --git a/README.md b/README.md index 327bdfdc..11c0c8c4 100644 --- a/README.md +++ b/README.md @@ -646,6 +646,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | | 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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index fc1cb6cf..2e81571d 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '8.0.6' + s.version = '8.0.7' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/2429_minimize_xor.rb b/lib/medium/2429_minimize_xor.rb new file mode 100644 index 00000000..6958b2a9 --- /dev/null +++ b/lib/medium/2429_minimize_xor.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/minimize-xor/ +# @param {Integer} num1 +# @param {Integer} num2 +# @return {Integer} +def minimize_xor(num1, num2) + a = num1.to_s(2).count('1') + b = num2.to_s(2).count('1') + + result = num1 + + (0...32).each do |i| + if a > b && (1 << i) & num1 != 0 + result ^= 1 << i + a -= 1 + end + + if a < b && ((1 << i) & num1).zero? + result ^= 1 << i + a += 1 + end + end + + result +end diff --git a/test/medium/test_2429_minimize_xor.rb b/test/medium/test_2429_minimize_xor.rb new file mode 100644 index 00000000..01cde43d --- /dev/null +++ b/test/medium/test_2429_minimize_xor.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/2429_minimize_xor' +require 'minitest/autorun' + +class MinimizeXORTest < ::Minitest::Test + def test_default_one = assert_equal(3, minimize_xor(3, 5)) + + def test_default_two = assert_equal(3, minimize_xor(1, 12)) + + def test_additional_one = assert_equal(24, minimize_xor(25, 72)) +end