diff --git a/README.md b/README.md index 50f6e04f..964ad275 100644 --- a/README.md +++ b/README.md @@ -628,6 +628,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 841. Keys and Rooms | [Link](https://leetcode.com/problems/keys-and-rooms/) | [Link](./lib/medium/841_keys_and_rooms.rb) | [Link](./test/medium/test_841_keys_and_rooms.rb) | | 852. Peak Index in a Mountain Array | [Link](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Link](./lib/medium/852_peak_index_in_a_mountain_array.rb) | [Link](./test/medium/test_852_peak_index_in_a_mountain_array.rb) | | 856. Score of Parentheses | [Link](https://leetcode.com/problems/score-of-parentheses/) | [Link](./lib/medium/856_score_of_parentheses.rb) | [Link](./test/medium/test_856_score_of_parentheses.rb) | +| 858. Mirror Reflection | [Link](https://leetcode.com/problems/mirror-reflection/) | [Link](./lib/medium/858_mirror_reflection.rb) | [Link](./test/medium/test_858_mirror_reflection.rb) | ### Hard diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 66b8a700..e8ea5b15 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 = '7.6.9' + s.version = '7.7.0' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/858_mirror_reflection.rb b/lib/medium/858_mirror_reflection.rb new file mode 100644 index 00000000..96c92e21 --- /dev/null +++ b/lib/medium/858_mirror_reflection.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/mirror-reflection/ +# @param {Integer} p +# @param {Integer} q +# @return {Integer} +def mirror_reflection(p, q) + while p.even? && q.even? + p >>= 1 + q >>= 1 + end + + 1 - p % 2 + q % 2 +end diff --git a/test/medium/test_858_mirror_reflection.rb b/test/medium/test_858_mirror_reflection.rb new file mode 100644 index 00000000..236c4cf3 --- /dev/null +++ b/test/medium/test_858_mirror_reflection.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/858_mirror_reflection' +require 'minitest/autorun' + +class MirrorReflectionTest < ::Minitest::Test + def test_default_one = assert_equal(2, mirror_reflection(2, 1)) + + def test_default_two = assert_equal(1, mirror_reflection(3, 1)) + + def test_additional_one = assert_equal(1, mirror_reflection(2, 2)) +end