Advertisement
nikunjsoni

856

Mar 22nd, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int max(int a, int b){
  4.         return a > b ? a : b;
  5.     }
  6.     int scoreOfParentheses(string S) {
  7.         stack<int> s;
  8.         int res = 0;
  9.         for(char c: S){
  10.             if(c == '('){
  11.                 s.push(res);
  12.                 res = 0;
  13.             }
  14.             else{
  15.                 res = s.top() + max(res*2, 1);
  16.                 s.pop();
  17.             }
  18.         }
  19.         return res;
  20.     }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement