856. Score of Parentheses

```python class Solution: def scoreOfParentheses(self, s: str) -> int: st = [] for c in s: if c == ‘(‘: st.append(c) else: # c == ) if st[-1] == ‘(‘: st.pop() st.append(‘1’) elif st[-1].isdigit(): x = int(st.pop()) while st[-1].isdigit(): x += int(st.pop()) # AB => A + B x =... [Read More]
Tags: String Stack

164. Maximum Gap

Pigeon hole principle => need to make sure at least one bucket is empty ```python class Solution: def maximumGap(self, A: List[int]) -> int: “"”tc O(N) sc O(N) main idea: since is required to write in tc O(N) sc O(N) since need to get max gap for each consecutive pair =>... [Read More]
Tags: Sort

1665. Minimum Initial Energy to Finish Tasks

```python class Solution: def minimumEffort(self, tasks: List[List[int]]) -> int: “”” TODO: thought: 1. binary search left- sum(actual ) right: sum(minimun) tc O(NlgN) sc O(1) each time remaining saved = mmin - cost, next time mmin = original mmin- prev_saved we need to maximize each time saved, so max(mmin-cost) = >... [Read More]
Tags: Greedy

1402. Reducing Dishes

class Solution: def maxSatisfaction(self, A: List[int]) -> int: """ tc O(NlgN) sc O(1) main idea: start with the biggest satisfaction level; as only as current accumutive satisfaction >0, the longer biggest dish stays, the more coefficiency total will get => need to track (1) accumutive satisfaction : = total +... [Read More]
Tags: Greedy

1520. Maximum Number of Non-Overlapping Substrings

```python class Solution: def maxNumOfSubstrings(self, s: str) -> List[str]: “”” main idea: this is a finding list of minimal non-overlapping interval problem. In order to avoid sorting(based on index), we use a left, right varible to track range for each char. And then use greedy to get the minimal length... [Read More]
Tags: Greedy