From 9f1183eba644ff8819bfceb5097280037e830541 Mon Sep 17 00:00:00 2001 From: fartem Date: Thu, 13 Feb 2025 07:38:49 +0300 Subject: [PATCH] 2025-02-13 v. 8.5.3: added "1381. Design a Stack With Increment Operation" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...design_a_stack_with_increment_operation.rb | 45 +++++++++++++++++++ ...design_a_stack_with_increment_operation.rb | 27 +++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/medium/1381_design_a_stack_with_increment_operation.rb create mode 100644 test/medium/test_1381_design_a_stack_with_increment_operation.rb diff --git a/README.md b/README.md index eb8e2a03..10aeb7b0 100644 --- a/README.md +++ b/README.md @@ -686,6 +686,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1358. Number of Substrings Containing All Three Characters | [Link](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/) | [Link](./lib/medium/1358_number_of_substrings_containing_all_three_characters.rb) | [Link](./test/medium/test_1358_number_of_substrings_containing_all_three_characters.rb) | | 1367. Linked List in Binary Tree | [Link](https://leetcode.com/problems/linked-list-in-binary-tree/) | [Link](./lib/medium/1367_linked_list_in_binary_tree.rb) | [Link](./test/medium/test_1367_linked_list_in_binary_tree.rb) | | 1376. Time Needed to Inform All Employees | [Link](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [Link](./lib/medium/1376_time_needed_to_inform_all_employees.rb) | [Link](./test/medium/test_1376_time_needed_to_inform_all_employees.rb) | +| 1381. Design a Stack With Increment Operation | [Link](https://leetcode.com/problems/design-a-stack-with-increment-operation/) | [Link](./lib/medium/1381_design_a_stack_with_increment_operation.rb) | [Link](./test/medium/test_1381_design_a_stack_with_increment_operation.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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 2caa0c04..9afc382b 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.5.3' + s.version = '8.5.4' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1381_design_a_stack_with_increment_operation.rb b/lib/medium/1381_design_a_stack_with_increment_operation.rb new file mode 100644 index 00000000..b8b39eb7 --- /dev/null +++ b/lib/medium/1381_design_a_stack_with_increment_operation.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/design-a-stack-with-increment-operation/ +class CustomStack + # @param {Integer} max_size + def initialize(max_size) + @stack = ::Array.new(max_size, 0) + @max_size = max_size + @size = 0 + end + + # @param {Integer} x + # @return {Void} + def push(x) + return unless @size < @max_size + + @stack[@size] = x + @size += 1 + end + + # @return {Integer} + def pop + return -1 unless @size.positive? + + @size -= 1 + @stack[@size] + end + + # @param {Integer} k + # @param {Integer} val + # @return {Void} + def increment(k, val) + if @size < k + (0...@size).each { |i| @stack[i] += val } + else + p = 0 + + while k.positive? + @stack[p] += val + p += 1 + k -= 1 + end + end + end +end diff --git a/test/medium/test_1381_design_a_stack_with_increment_operation.rb b/test/medium/test_1381_design_a_stack_with_increment_operation.rb new file mode 100644 index 00000000..0e3f6be3 --- /dev/null +++ b/test/medium/test_1381_design_a_stack_with_increment_operation.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1381_design_a_stack_with_increment_operation' +require 'minitest/autorun' + +class DesignAStackWithIncrementOperationTest < ::Minitest::Test + def test_default_one + stack = ::CustomStack.new(3) + stack.push(1) + stack.push(2) + + assert_equal(2, stack.pop) + + stack.push(2) + stack.push(3) + stack.push(4) + + stack.increment(5, 100) + stack.increment(2, 100) + + assert_equal(103, stack.pop) + assert_equal(202, stack.pop) + assert_equal(201, stack.pop) + assert_equal(-1, stack.pop) + end +end