Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] authored and romankurnovskii committed Apr 15, 2024
1 parent 0fc7a60 commit 4b0299c
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 104 deletions.
15 changes: 8 additions & 7 deletions content/tracks/algorithms-101/leetcode/easy/1614/index.en.md
Expand Up @@ -5,8 +5,8 @@ description: 1614. Maximum Nesting Depth of the Parentheses
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
date: 2024-04-04
lastMod: 2024-04-04
featuredImage: https://picsum.photos/700/155?grayscale
weight: 1614
---
Expand All @@ -16,13 +16,14 @@ weight: 1614
```python
class Solution:
def maxDepth(self, s: str) -> int:
res = d = 0
stack = []
res = 0
for c in s:
if c == '(':
d += 1
res = max(res, d)
stack.append(1)
res = max(res, len(stack))
elif c == ')':
d -= 1
stack.pop()

return res

```
15 changes: 8 additions & 7 deletions content/tracks/algorithms-101/leetcode/easy/1614/index.ru.md
Expand Up @@ -5,8 +5,8 @@ description: 1614. Maximum Nesting Depth of the Parentheses
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
date: 2024-04-04
lastMod: 2024-04-04
featuredImage: https://picsum.photos/700/155?grayscale
weight: 1614
---
Expand All @@ -16,13 +16,14 @@ weight: 1614
```python
class Solution:
def maxDepth(self, s: str) -> int:
res = d = 0
stack = []
res = 0
for c in s:
if c == '(':
d += 1
res = max(res, d)
stack.append(1)
res = max(res, len(stack))
elif c == ')':
d -= 1
stack.pop()

return res

```
53 changes: 53 additions & 0 deletions content/tracks/algorithms-101/leetcode/medium/1249/index.en.md
@@ -0,0 +1,53 @@
---
title: 1249. Minimum Remove to Make Valid Parentheses
seoTitle: LeetCode 1249. Minimum Remove to Make Valid Parentheses | Python solution and explanation
description: 1249. Minimum Remove to Make Valid Parentheses
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 1249
---

[LeetCode problem 1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)

## Approach

The problem can be solved in two passes using a stack for the first pass and a simple iteration for the second:

1. First Pass (Identify Invalid Parentheses):

- Iterate through the string, and for every character:
- If it's '(', push its index onto the stack.
- If it's ')' and the stack is not empty (there is a matching '('), pop from the stack. Otherwise, mark this ')' as invalid.
- After the iteration, any indices remaining in the stack represent unmatched '(' that should be removed.

2. Second Pass (Build the Result String):

- Iterate through the string again, building the result string by including characters that are not marked as invalid.

```python
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
stack = [] # Stack to keep track of the indices of '('
remove_indices = set() # Set to keep track of indices to remove

for i, char in enumerate(s): # First pass to identify invalid parentheses
if char == '(':
stack.append(i)
elif char == ')':
if stack:
stack.pop()
else:
remove_indices.add(i)

# Add indices of remaining '(' to remove
remove_indices = remove_indices.union(set(stack))

# Second pass to build the result string
result = [s[i] for i in range(len(s)) if i not in remove_indices]

return ''.join(result)
```
53 changes: 53 additions & 0 deletions content/tracks/algorithms-101/leetcode/medium/1249/index.ru.md
@@ -0,0 +1,53 @@
---
title: 1249. Minimum Remove to Make Valid Parentheses
seoTitle: LeetCode 1249. Minimum Remove to Make Valid Parentheses | Python solution and explanation
description: 1249. Minimum Remove to Make Valid Parentheses
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 1249
---

[LeetCode problem 1249](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)

## Approach

The problem can be solved in two passes using a stack for the first pass and a simple iteration for the second:

1. First Pass (Identify Invalid Parentheses):

- Iterate through the string, and for every character:
- If it's '(', push its index onto the stack.
- If it's ')' and the stack is not empty (there is a matching '('), pop from the stack. Otherwise, mark this ')' as invalid.
- After the iteration, any indices remaining in the stack represent unmatched '(' that should be removed.

2. Second Pass (Build the Result String):

- Iterate through the string again, building the result string by including characters that are not marked as invalid.

```python
class Solution:
def minRemoveToMakeValid(self, s: str) -> str:
stack = [] # Stack to keep track of the indices of '('
remove_indices = set() # Set to keep track of indices to remove

for i, char in enumerate(s): # First pass to identify invalid parentheses
if char == '(':
stack.append(i)
elif char == ')':
if stack:
stack.pop()
else:
remove_indices.add(i)

# Add indices of remaining '(' to remove
remove_indices = remove_indices.union(set(stack))

# Second pass to build the result string
result = [s[i] for i in range(len(s)) if i not in remove_indices]

return ''.join(result)
```
96 changes: 14 additions & 82 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@fullhuman/postcss-purgecss": "^5.0.0",
"@fullhuman/postcss-purgecss": "^6.0.0",
"@tailwindcss/typography": "^0.5.12",
"autoprefixer": "^10.4.19",
"cssnano": "^6.1.2",
Expand Down

0 comments on commit 4b0299c

Please sign in to comment.