본문 바로가기

전체 글267

1249. Minimum Remove to Make Valid Parentheses class Solution: def minRemoveToMakeValid(self, s: str) -> str: s = list(s) stack = [] for i, char in enumerate(s): if char == '(': stack.append(i) elif char == ')': if stack: stack.pop() else: s[i] = '' while stack: s[stack.pop()] = '' return ''.join(s) Convert string to list, because String is an immutable data structure in Python and it's much easier and memory-efficient to deal with a list fo.. 2020. 8. 4.
739. Daily Temperatures class Solution: def dailyTemperatures(self, T: List[int]) -> List[int]: stack = [] res = [0]*len(T) for i, temperature in enumerate(T): while stack and temperature > T[stack[-1]]: index = stack.pop() res[index] = i - index # write answer stack.append(i) return res 2020. 8. 3.
CS50 Week 3 Linear search Binary search Bubble sort Selection sort Insertion sort Recursion Merge sort Bubble Sort def bubblesort(data): for index in range(len(data) - 1): for index2 in range(len(data) - index - 1): if data[index2] > data[index2 + 1]: data[index2], data[index2 + 1] = data[index2 + 1], data[index2] return data 반복문이 두 개 O(n2) 최악의 경우, n∗(n−1)2 완전 정렬이 되어 있는 상태라면 최선은 O(n) Selection Sort def sele.. 2020. 7. 27.
921. Minimum Add to Make Parentheses Valid 1. The time complexity is Θ(n²) in the worst case. class Solution(object): def minAddToMakeValid(self, S): """ :type S: str :rtype: int """ while "()" in S: S = S.replace("()", "") return len(S) 2. Easy to understand class Solution(object): def minAddToMakeValid(self, S): """ :type S: str :rtype: int """ stack = [] count = 0 for i in S: if i == '(': stack.append(i) elif stack: stack.pop() else: .. 2020. 7. 22.
20. Valid Parentheses class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ # The stack to keep track of opening brackets. stack = [] # Hash map for keeping track of mappings. This keeps the code very clean. # Also makes adding more types of parenthesis easier mapping = {")": "(", "}": "{", "]": "["} # For every bracket in the expression. for char in s: # If the character is an closing brack.. 2020. 7. 21.
844. Backspace String Compare 다른 사람 솔루션 class Solution: def backspaceCompare(self, S: str, T: str) -> bool: ans = '' ans_2 = '' for i in S: if i == '#': ans = ans[:-1] else: ans += i for i in T: if i == '#': ans_2 = ans_2[:-1] else: ans_2 += i return ans == ans_2 list가 아닌 string을 활용해서 stack 2020. 7. 21.
682. Baseball Game class Solution: def calPoints(self, ops: List[str]) -> int: a=[] for i in ops: if i=='C': a.pop() elif i=='D': a.append(a[-1]*2) elif i=='+': a.append(a[-1]+a[-2]) else: a.append(int(i)) return sum(a) int 조건을 처음으로 하기보다는 맨 마지막으로 넣어서 2020. 7. 19.
496. Next Greater Element I Wrong Answer class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: res = [] for i in range(len(nums1)): idx = nums2.index(nums1[i]) # error if last value in num2 == num1's some if nums2[-1] == nums2[idx]: # if match last value res.append(-1) elif nums2[idx] List[int]: # nums1 is a subset of nums2 result = [] for i, n in list(enumerate(nums1)): ind = nums2.. 2020. 7. 6.
1047. Remove All Adjacent Duplicates In String class Solution: def removeDuplicates(self, S: str) -> str: check = [] for s in S: if len(check)>0 and check[-1] == s: check.pop() else: check.append(s) return "".join(check) pop을 사용해야 겠다고는 생각했지만 if len(check)>0 and check[-1] == s 이 조건문을 만드는 것이 가장 어려운 부분이였다. 이번에도 혼자서 해결이 안되서 보고 풀었다.. 출력을 해보면 s check a a b a, b b a a [] c c a c, a 이게 머릿속에 빠르게 입력이 안되는게 답답 2020. 7. 5.