<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p>

<p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;1 + 1&quot;
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot; 2-1 + 2 &quot;
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(1+(4+5+2)-3)+(6+8)&quot;
<strong>Output:</strong> 23
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of digits, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, and <code>&#39; &#39;</code>.</li>
	<li><code>s</code> represents a valid expression.</li>
	<li><code>&#39;+&#39;</code> is <strong>not</strong> used as a unary operation (i.e., <code>&quot;+1&quot;</code> and <code>&quot;+(2 + 3)&quot;</code> is invalid).</li>
	<li><code>&#39;-&#39;</code> could be used as a unary operation (i.e., <code>&quot;-1&quot;</code> and <code>&quot;-(2 + 3)&quot;</code> is valid).</li>
	<li>There will be no two consecutive operators in the input.</li>
	<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>

class Solution:
    def calculate(self, s: str) -> int:
        # 1. 연산자와 숫자로만 이루어진 문자열 list setting
        # 2. 스택 조작
        # - (: push
        # - 숫: (top이 (: 같이 푸시), (top이 +- 에다가 하나 더 있다면 연산, 없으면 그냥 +-만 적용)
        # - ): 숫자 pop 해서 연산

        # 아니면 재귀 형식?
        # - (: close 짝 위치 매핑
        # def dfs(lo, hi):
        #   만약 s[i]가 (라면, dfs(i + 1, 매핑된 위치 - 1)
        #   이렇게 괄호 모두 제거한 dfs에서 반복하면서 연산
        pmap = {}
        stack = []
        

        def makegroup(s) -> list[str]:
            new_char = ""
            g = []
            for i in range(len(s)):
                if s[i] == ' ': continue
                if s[i] in '+-()':
                    if new_char:
                        g.append(new_char)
                        new_char = ""
                    g.append(s[i])
                else:
                    new_char += s[i]
            
            if new_char:
                g.append(new_char)
            return g
        
        g = makegroup(s)
        for i in range(len(g)):
            char = g[i]
            if char == ')':
                pos = stack.pop()
                pmap[pos] = i
            elif char == '(':
                stack.append(i)

        print(g)

        def dfs(lo, hi) -> str:
            new_stack = []
            i = lo
            while i < hi + 1:
                if g[i] in '+-':
                    new_stack.append(g[i])
                else:
                    if g[i] == '(':
                        num = dfs(i + 1, pmap[i] - 1)
                        i = pmap[i]
                    else:
                        num = g[i]
                    
                    slen = len(new_stack)
                    if slen == 0:
                        new_stack.append(num)
                    elif slen == 1:
                        op = new_stack.pop()
                        if op == '-':
                            new_stack.append(str(-int(num)))
                        else:
                            new_stack.append(num)
                    else: # slen > 1
                        op = new_stack.pop()
                        intnum = int(new_stack.pop())
                        if op == '-':
                            new_stack.append(str(intnum - int(num)))
                        else:
                            new_stack.append(str(intnum + int(num)))

                i += 1
            return new_stack[0]

        return int(dfs(0, len(g) - 1))

22744