File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 663
663
853|[ Car Fleet] ( ./0853-car-fleet.js ) |Medium|
664
664
854|[ K-Similar Strings] ( ./0854-k-similar-strings.js ) |Hard|
665
665
855|[ Exam Room] ( ./0855-exam-room.js ) |Medium|
666
+ 856|[ Score of Parentheses] ( ./0856-score-of-parentheses.js ) |Medium|
666
667
867|[ Transpose Matrix] ( ./0867-transpose-matrix.js ) |Easy|
667
668
868|[ Binary Gap] ( ./0868-binary-gap.js ) |Easy|
668
669
872|[ Leaf-Similar Trees] ( ./0872-leaf-similar-trees.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments