Minimum Add to Make Parentheses Valid - Leetcode 921 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
5:34 - Coding Explanation

leetcode 921

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Was confused by the problem description, came here to understand it and solved it. Thank you neetcode

kermatt
Автор

high key, watching your videos is so therapeutic

anshumansingh
Автор

Yesterday's problem was interesting than this, this seems easy after solving yday problem.

nirmalgurjar
Автор

Always loved the way you explain the concept. Would be glad if you can upload a video on segment Tree and it's lazy propagation.

amanaryan
Автор

This problem already we did right in this month

MyLuck
Автор

Code can be simplified a bit
class Solution(object):
def minAddToMakeValid(self, s):
open_count = result = 0

for c in s:
if c == "(":
open_count+=1
elif open_count:
open_count-=1
else:
result+=1

return open_count+result

kewlvk
Автор

using stack



class Solution {
public:
int minAddToMakeValid(string s) {
stack<char> st;
int cnt =0;
for(int i = 0;i<s.size();i++){
if(s[i] == '('){
st.push(s[i]);
}else if(!st.empty() && s[i] == ')'){
st.pop();
}else{
cnt++;
}
}
return cnt+st.size();
}
};

vijayarun_
Автор

Love the videos man, gotta lock in for recruting this semester 😭
430. Flatten a Multilevel Doubly Linked List
if you can could you solve the above question?

advolv
Автор

The problem description was more confusing than the solution. Actually the solution is preety much feels easy problem.

LankeshM-reop
Автор

Guy just called more than half of his viewers NOOB lol

abhimanyuambastha
Автор

I did this by myself, my code may not be the best but I think it was using the yesterday questions logic.

class Solution:
def minAddToMakeValid(self, s: str) -> int:
open_, close = 0, 0
adds = 0

for c in s:
if c == '(':
open_ += 1
else:
close += 1

if close > open_:
adds += 1
close -= 1

if open_ > close:
adds += open_ - close

return adds

chaitanyayeole
welcome to shbcf.ru