Skip to content

Commit df7894e

Browse files
authored
2025-01-03 v. 7.6.7: added "856. Score of Parentheses"
2 parents 95cd7bd + 0404eb8 commit df7894e

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
627627
| 823. Binary Trees With Factors | [Link](https://leetcode.com/problems/binary-trees-with-factors/) | [Link](./lib/medium/823_binary_trees_with_factors.rb) | [Link](./test/medium/test_823_binary_trees_with_factors.rb) |
628628
| 841. Keys and Rooms | [Link](https://leetcode.com/problems/keys-and-rooms/) | [Link](./lib/medium/841_keys_and_rooms.rb) | [Link](./test/medium/test_841_keys_and_rooms.rb) |
629629
| 852. Peak Index in a Mountain Array | [Link](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Link](./lib/medium/852_peak_index_in_a_mountain_array.rb) | [Link](./test/medium/test_852_peak_index_in_a_mountain_array.rb) |
630+
| 856. Score of Parentheses | [Link](https://leetcode.com/problems/score-of-parentheses/) | [Link](./lib/medium/856_score_of_parentheses.rb) | [Link](./test/medium/test_856_score_of_parentheses.rb) |
630631

631632
### Hard
632633

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 = '7.6.6'
8+
s.version = '7.6.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/score-of-parentheses/
4+
# @param {String} s
5+
# @return {Integer}
6+
def score_of_parentheses(s)
7+
stack = [0]
8+
s.each_char do |c|
9+
if c == '('
10+
stack << 0
11+
else
12+
a = stack.delete_at(stack.size - 1)
13+
b = stack.delete_at(stack.size - 1)
14+
15+
stack << b + [2 * a, 1].max
16+
end
17+
end
18+
19+
stack.last
20+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/856_score_of_parentheses'
5+
require 'minitest/autorun'
6+
7+
class ScoreOfParenthesesTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
1,
11+
score_of_parentheses(
12+
'()'
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
2,
20+
score_of_parentheses(
21+
'(())'
22+
)
23+
)
24+
end
25+
26+
def test_default_three
27+
assert_equal(
28+
2,
29+
score_of_parentheses(
30+
'()()'
31+
)
32+
)
33+
end
34+
end

0 commit comments

Comments
 (0)