Photo by Pietro Mattia on Unsplash
from collections import deque
def solution(s):
check_right_parentheses = deque(list(s))
last_parenth = []
while check_right_parentheses:
cur_parenth = check_right_parentheses.popleft()
if len(last_parenth) == 0 and cur_parenth == ')':
return False
elif len(last_parenth) !=0 and cur_parenth != last_parenth[-1]:
last_parenth.pop()
else:
last_parenth.append(cur_parenth)
if len(last_parenth) > 0:
return False
return True
def dfs(visited, computers, node):
if visited[node] == 0:
visited[node] = 1
for idx, is_network in enumerate(computers[node]):
if visited[idx] == 0 and is_network:
dfs(visited, computers, idx)
return True
return False
def solution(n, computers):
cnt = 0
visited = [0] * n
for i in range(n):
if dfs(visited, computers, i):
cnt+=1
return cnt
import heapq
def solution(scoville, K):
cnt = 0
heapq.heapify(scoville)
while scoville:
first = heapq.heappop(scoville)
if first >= K: return cnt
if len(scoville) == 0: return -1
second = heapq.heappop(scoville)
new_food = (first + (second*2))
scoville.append(new_food)
cnt+=1
return -1