Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
45 changes: 45 additions & 0 deletions lib/medium/1381_design_a_stack_with_increment_operation.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions test/medium/test_1381_design_a_stack_with_increment_operation.rb
Original file line number Diff line number Diff line change
@@ -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