Skip to content

Commit cb5cf7f

Browse files
committed
Add solution #856
1 parent 42fcc21 commit cb5cf7f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@
663663
853|[Car Fleet](./0853-car-fleet.js)|Medium|
664664
854|[K-Similar Strings](./0854-k-similar-strings.js)|Hard|
665665
855|[Exam Room](./0855-exam-room.js)|Medium|
666+
856|[Score of Parentheses](./0856-score-of-parentheses.js)|Medium|
666667
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
667668
868|[Binary Gap](./0868-binary-gap.js)|Easy|
668669
872|[Leaf-Similar Trees](./0872-leaf-similar-trees.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 856. Score of Parentheses
3+
* https://leetcode.com/problems/score-of-parentheses/
4+
* Difficulty: Medium
5+
*
6+
* Given a balanced parentheses string s, return the score of the string.
7+
*
8+
* The score of a balanced parentheses string is based on the following rule:
9+
* - "()" has score 1.
10+
* - AB has score A + B, where A and B are balanced parentheses strings.
11+
* - (A) has score 2 * A, where A is a balanced parentheses string.
12+
*/
13+
14+
/**
15+
* @param {string} s
16+
* @return {number}
17+
*/
18+
var scoreOfParentheses = function(s) {
19+
const stack = [0];
20+
21+
for (const char of s) {
22+
if (char === '(') {
23+
stack.push(0);
24+
} else {
25+
const value = stack.pop();
26+
stack[stack.length - 1] += Math.max(2 * value, 1);
27+
}
28+
}
29+
30+
return stack[0];
31+
};

0 commit comments

Comments
 (0)