Skip to content

Commit 9f1183e

Browse files
committed
2025-02-13 v. 8.5.3: added "1381. Design a Stack With Increment Operation"
1 parent 06ae290 commit 9f1183e

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
686686
| 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) |
687687
| 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) |
688688
| 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) |
689+
| 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) |
689690
| 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) |
690691
| 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) |
691692
| 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) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.5.3'
8+
s.version = '8.5.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/design-a-stack-with-increment-operation/
4+
class CustomStack
5+
# @param {Integer} max_size
6+
def initialize(max_size)
7+
@stack = ::Array.new(max_size, 0)
8+
@max_size = max_size
9+
@size = 0
10+
end
11+
12+
# @param {Integer} x
13+
# @return {Void}
14+
def push(x)
15+
return unless @size < @max_size
16+
17+
@stack[@size] = x
18+
@size += 1
19+
end
20+
21+
# @return {Integer}
22+
def pop
23+
return -1 unless @size.positive?
24+
25+
@size -= 1
26+
@stack[@size]
27+
end
28+
29+
# @param {Integer} k
30+
# @param {Integer} val
31+
# @return {Void}
32+
def increment(k, val)
33+
if @size < k
34+
(0...@size).each { |i| @stack[i] += val }
35+
else
36+
p = 0
37+
38+
while k.positive?
39+
@stack[p] += val
40+
p += 1
41+
k -= 1
42+
end
43+
end
44+
end
45+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1381_design_a_stack_with_increment_operation'
5+
require 'minitest/autorun'
6+
7+
class DesignAStackWithIncrementOperationTest < ::Minitest::Test
8+
def test_default_one
9+
stack = ::CustomStack.new(3)
10+
stack.push(1)
11+
stack.push(2)
12+
13+
assert_equal(2, stack.pop)
14+
15+
stack.push(2)
16+
stack.push(3)
17+
stack.push(4)
18+
19+
stack.increment(5, 100)
20+
stack.increment(2, 100)
21+
22+
assert_equal(103, stack.pop)
23+
assert_equal(202, stack.pop)
24+
assert_equal(201, stack.pop)
25+
assert_equal(-1, stack.pop)
26+
end
27+
end

0 commit comments

Comments
 (0)