From dcc9b2d74b700e3ec8d5134f8f73a0b6f1dbe550 Mon Sep 17 00:00:00 2001 From: fartem Date: Fri, 4 Apr 2025 07:23:01 +0300 Subject: [PATCH] 2025-03-04 v. 9.1.9: added "2375. Construct Smallest Number From DI String" --- .rubocop.yml | 2 ++ Gemfile | 1 + README.md | 1 + leetcode-ruby.gemspec | 2 +- ...onstruct_smallest_number_from_di_string.rb | 17 +++++++++++++ ...onstruct_smallest_number_from_di_string.rb | 25 +++++++++++++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 lib/medium/2375_construct_smallest_number_from_di_string.rb create mode 100644 test/medium/test_2375_construct_smallest_number_from_di_string.rb diff --git a/.rubocop.yml b/.rubocop.yml index 6c3ef322..bfb66e02 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -22,6 +22,8 @@ Style/DisableCopsWithinSourceCodeDirective: Enabled: false Style/IpAddresses: Enabled: false +Style/NestedModifier: + Enabled: false Bundler/GemComment: Enabled: false diff --git a/Gemfile b/Gemfile index 57069f0e..f996953b 100644 --- a/Gemfile +++ b/Gemfile @@ -8,3 +8,4 @@ gem 'prime', '0.1.2' gem 'rake', '12.3.3' gem 'rubocop', '1.7.0' gem 'simplecov', '0.22.0' +gem 'solargraph', '0.44.0' diff --git a/README.md b/README.md index 79a85f7d..d18333b6 100644 --- a/README.md +++ b/README.md @@ -753,6 +753,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 2266. Count Number of Texts | [Link](https://leetcode.com/problems/count-number-of-texts/) | [Link](./lib/medium/2266_count_number_of_texts.rb) | [Link](./test/medium/test_2266_count_number_of_texts.rb) | | 2295. Replace Elements in an Array | [Link](https://leetcode.com/problems/replace-elements-in-an-array/) | [Link](./lib/medium/2295_replace_elements_in_an_array.rb) | [Link](./test/medium/test_2295_replace_elements_in_an_array.rb) | | 2326. Spiral Matrix IV | [Link](https://leetcode.com/problems/spiral-matrix-iv/) | [Link](./lib/medium/2326_spiral_matrix_iv.rb) | [Link](./test/medium/test_2326_spiral_matrix_iv.rb) | +| 2375. Construct Smallest Number From DI String | [Link](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | [Link](./lib/medium/2375_construct_smallest_number_from_di_string.rb) | [Link](./test/medium/test_2375_construct_smallest_number_from_di_string.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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 98ca6d54..cc7b8dd3 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 = '9.1.8' + s.version = '9.1.9' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/2375_construct_smallest_number_from_di_string.rb b/lib/medium/2375_construct_smallest_number_from_di_string.rb new file mode 100644 index 00000000..185e92ca --- /dev/null +++ b/lib/medium/2375_construct_smallest_number_from_di_string.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/construct-smallest-number-from-di-string/ +# @param {String} pattern +# @return {String} +def smallest_number2375(pattern) + stack = [1] + result = '' + + (0..pattern.size).each do |i| + result += stack.pop.to_s until stack.empty? if i == pattern.size || pattern[i] == 'I' + + stack.push(i + 2) + end + + result +end diff --git a/test/medium/test_2375_construct_smallest_number_from_di_string.rb b/test/medium/test_2375_construct_smallest_number_from_di_string.rb new file mode 100644 index 00000000..ed5eab76 --- /dev/null +++ b/test/medium/test_2375_construct_smallest_number_from_di_string.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/2375_construct_smallest_number_from_di_string' +require 'minitest/autorun' + +class ConstructSmallestNumberFromDIStringTest < ::Minitest::Test + def test_default_one + assert_equal( + '123549876', + smallest_number2375( + 'IIIDIDDD' + ) + ) + end + + def test_default_two + assert_equal( + '4321', + smallest_number2375( + 'DDD' + ) + ) + end +end