diff --git a/README.md b/README.md index c211be5a..d8cecc1e 100644 --- a/README.md +++ b/README.md @@ -718,6 +718,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1797. Design Authentication Manager | [Link](https://leetcode.com/problems/design-authentication-manager/) | [Link](./lib/medium/1797_design_authentication_manager.rb) | [Link](./test/medium/test_1797_design_authentication_manager.rb) | | 1814. Count Nice Pairs in an Array | [Link](https://leetcode.com/problems/count-nice-pairs-in-an-array/) | [Link](./lib/medium/1814_count_nice_pairs_in_an_array.rb) | [Link](./test/medium/test_1814_count_nice_pairs_in_an_array.rb) | | 1823. Find the Winner of the Circular Game | [Link](https://leetcode.com/problems/find-the-winner-of-the-circular-game/) | [Link](./lib/medium/1823_find_the_winner_of_the_circular_game.rb) | [Link](./test/medium/test_1823_find_the_winner_of_the_circular_game.rb) | +| 1829. Maximum XOR for Each Query | [Link](https://leetcode.com/problems/maximum-xor-for-each-query/) | [Link](./lib/medium/1829_maximum_xor_for_each_query.rb) | [Link](./test/medium/test_1829_maximum_xor_for_each_query.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) | | 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 831d9ecd..6f0ba01a 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.8.4' + s.version = '8.8.5' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1829_maximum_xor_for_each_query.rb b/lib/medium/1829_maximum_xor_for_each_query.rb new file mode 100644 index 00000000..c1c01e9e --- /dev/null +++ b/lib/medium/1829_maximum_xor_for_each_query.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/maximum-xor-for-each-query/ +# @param {Integer[]} nums +# @param {Integer} maximum_bit +# @return {Integer[]} +def get_maximum_xor(nums, maximum_bit) + result = ::Array.new(nums.size, 0) + xor = (1 << maximum_bit) - 1 + + (0...nums.size).each do |i| + xor ^= nums[i] + result[nums.size - i - 1] = xor + end + + result +end diff --git a/test/medium/test_1829_maximum_xor_for_each_query.rb b/test/medium/test_1829_maximum_xor_for_each_query.rb new file mode 100644 index 00000000..e89106c4 --- /dev/null +++ b/test/medium/test_1829_maximum_xor_for_each_query.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1829_maximum_xor_for_each_query' +require 'minitest/autorun' + +class MaximumXORForEachQueryTest < ::Minitest::Test + def test_default_one + assert_equal( + [0, 3, 2, 3], + get_maximum_xor( + [0, 1, 1, 3], + 2 + ) + ) + end + + def test_default_two + assert_equal( + [5, 2, 6, 5], + get_maximum_xor( + [2, 3, 4, 7], + 3 + ) + ) + end + + def test_default_three + assert_equal( + [4, 3, 6, 4, 6, 7], + get_maximum_xor( + [0, 1, 2, 2, 5, 7], + 3 + ) + ) + end +end