diff --git a/README.md b/README.md index fd5954a8..06d499db 100644 --- a/README.md +++ b/README.md @@ -702,6 +702,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1492. The kth Factor of n | [Link](https://leetcode.com/problems/the-kth-factor-of-n/) | [Link](./lib/medium/1492_the_kth_factor_of_n.rb) | [Link](./test/medium/test_1492_the_kth_factor_of_n.rb) | | 1504. Count Submatrices With All Ones | [Link](https://leetcode.com/problems/count-submatrices-with-all-ones/) | [Link](./lib/medium/1504_count_submatrices_with_all_ones.rb) | [Link](./test/medium/test_1504_count_submatrices_with_all_ones.rb) | | 1525. Number of Good Ways to Split a String | [Link](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [Link](./lib/medium/1525_number_of_good_ways_to_split_a_string.rb) | [Link](./test/medium/test_1525_number_of_good_ways_to_split_a_string.rb) | +| 1529. Minimum Suffix Flips | [Link](https://leetcode.com/problems/minimum-suffix-flips/) | [Link](./lib/medium/1529_minimum_suffix_flips.rb) | [Link](./test/medium/test_1529_minimum_suffix_flips.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 28b2988c..43824e1c 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.6.8' + s.version = '8.6.9' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1529_minimum_suffix_flips.rb b/lib/medium/1529_minimum_suffix_flips.rb new file mode 100644 index 00000000..c145c295 --- /dev/null +++ b/lib/medium/1529_minimum_suffix_flips.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/minimum-suffix-flips/ +# @param {String} target +# @return {Integer} +def min_flips(target) + flip = '0' + result = 0 + + (0...target.size).each do |i| + next if target[i] == flip + + flip = target[i] + result += 1 + end + + result +end diff --git a/test/medium/test_1529_minimum_suffix_flips.rb b/test/medium/test_1529_minimum_suffix_flips.rb new file mode 100644 index 00000000..47fb1d1a --- /dev/null +++ b/test/medium/test_1529_minimum_suffix_flips.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1529_minimum_suffix_flips' +require 'minitest/autorun' + +class MinimumSuffixFlipsTest < ::Minitest::Test + def test_default_one = assert_equal(3, min_flips('10111')) + + def test_default_two = assert_equal(3, min_flips('101')) +end