전체 글

전체 글

    HashTable - Two Sum (과거인덱스, 지금인덱스)

    def solution(nums, target): #시간 복잡도 O(n^2) for i in range(len(nums)-1): for j in range(i+1, len(nums)): if nums[i]+nums[j]==target: return True else: return False def solution(nums, target): nums.sort() #O(nlogn) 정렬의 시간복잡도 x = 0 y = len(nums)-1 while(x

    Daily Temperatures - stack문제

    def dailyTemperatures(temperatures): ans = [0] * len(temperatures) #temperatures리스트를 다 순회했을 때 0을 반환하기 위함 #out of index를 방지하기 위함 stack = [] for cur_day, cur_temp in enumerate(temperatures): while stack and stack[-1][1] < cur_temp: prev_day, _ = stack.pop() #튜플을 꺼낼 때 한쪽값은 무시하고, 다른 한쪽 값만 뽑기 ans[prev_day] = cur_day - prev_day stack.append((cur_day, cur_temp )) return ans print(dailyTemperatures([73,..

    올바른 괄호 - 심화(스택 문제)

    def solution(s): stack = [] # 스택 초기화 for ch in s: # 문자열 s를 한 글자씩 반복해서 읽음 if ch in ['(', '[', '{']: # 여는 괄호라면 스택에 추가 stack.append(ch) else: # 닫는 괄호인 경우 if(len(stack)==0): # 닫는 괄호만 남아 있어서!! stack이 비어서 올바르지 않은 괄호 문자열인 경우, return False if ch == ')' and stack[-1] == '(': # 괄호 쌍이 맞는 경우 스택에서 제거(pop) stack.pop() elif ch == ']' and stack[-1] == '[': stack.pop() elif ch == '}' and stack[-1] == '{': stack...