Skip to content

Commit 68a87cb

Browse files
committed
feat (minremove): obvious speed issues removed
more changes seem to mean moving to idea of immediate editing list
1 parent 46d5e5f commit 68a87cb

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

leetcode/minremoveparens/minremoveparens.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
class Solution:
66
def minRemoveToMakeValid(self, s: str) -> str:
77
open_idx = []
8-
close_idx = []
98
remove_idx = []
109
ct = 0
1110
for idx, c in enumerate(s):
1211
if c == "(":
1312
open_idx.append(idx)
1413
ct += 1
1514
if c == ")":
16-
close_idx.append(idx)
1715
if ct == 0:
1816
remove_idx.append(idx)
1917
else:
2018
ct -= 1
2119

22-
for _ in range(ct):
23-
remove_idx.append(open_idx.pop())
20+
if ct > 0:
21+
remove_idx.extend(open_idx[-ct:])
2422

2523
return "".join(c for idx, c in enumerate(s) if not idx in remove_idx)
2624

0 commit comments

Comments
 (0)