From f0063ab9a3f57aa70f7333d276b0fa6e5ff2b20b Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 13 Jan 2025 11:01:20 +0300 Subject: [PATCH] 2025-01-13 v. 7.9.6: added "921. Minimum Add to Make Parentheses Valid" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...1_minimum_add_to_make_parentheses_valid.rb | 23 +++++++++++++++++++ ...1_minimum_add_to_make_parentheses_valid.rb | 11 +++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lib/medium/921_minimum_add_to_make_parentheses_valid.rb create mode 100644 test/medium/test_921_minimum_add_to_make_parentheses_valid.rb diff --git a/README.md b/README.md index aa2904ea..dd8f58b8 100644 --- a/README.md +++ b/README.md @@ -641,6 +641,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 912. Sort an Array | [Link](https://leetcode.com/problems/sort-an-array/) | [Link](./lib/medium/912_sort_an_array.rb) | [Link](./test/medium/test_912_sort_an_array.rb) | | 916. Word Subsets | [Link](https://leetcode.com/problems/word-subsets/) | [Link](./lib/medium/916_word_subsets.rb) | [Link](./test/medium/test_916_word_subsets.rb) | | 919. Complete Binary Tree Inserter | [Link](https://leetcode.com/problems/complete-binary-tree-inserter/) | [Link](./lib/medium/919_complete_binary_tree_inserter.rb) | [Link](./test/medium/test_919_complete_binary_tree_inserter.rb) | +| 921. Minimum Add to Make Parentheses Valid | [Link](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [Link](./lib/medium/921_minimum_add_to_make_parentheses_valid.rb) | [Link](./test/medium/test_921_minimum_add_to_make_parentheses_valid.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) | | 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 3390030e..24afc302 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.9.5' + s.version = '7.9.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/921_minimum_add_to_make_parentheses_valid.rb b/lib/medium/921_minimum_add_to_make_parentheses_valid.rb new file mode 100644 index 00000000..7a6d9cec --- /dev/null +++ b/lib/medium/921_minimum_add_to_make_parentheses_valid.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/ +# @param {String} s +# @return {Integer} +def min_add_to_make_valid(s) + o = 0 + c = 0 + + s.each_char do |char| + if char == ')' + if o.zero? + c += 1 + else + o -= 1 + end + else + o += 1 + end + end + + o + c +end diff --git a/test/medium/test_921_minimum_add_to_make_parentheses_valid.rb b/test/medium/test_921_minimum_add_to_make_parentheses_valid.rb new file mode 100644 index 00000000..be4b0bef --- /dev/null +++ b/test/medium/test_921_minimum_add_to_make_parentheses_valid.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/921_minimum_add_to_make_parentheses_valid' +require 'minitest/autorun' + +class MinimumAddToMakeParenthesesValidTest < ::Minitest::Test + def test_default_one = assert_equal(1, min_add_to_make_valid('())')) + + def test_default_two = assert_equal(3, min_add_to_make_valid('(((')) +end