본문 바로가기

Leetcode98

70. Climbing Stairs class Solution: def climbStairs(self, n: int) -> int: if(n==1): return 1 dp = [0] * (n+1) dp[1] = 1 dp[2] = 2 for i in range(3,n+1): dp[i] = dp[i-1] + dp[i-2] return dp[-1] 출처: leetcode.com/problems/climbing-stairs/discuss/685099/Python-Detailed-Explanation-DP-solution 2020. 8. 15.
392. Is Subsequence class Solution: def isSubsequence(self, s: str, t: str) -> bool: i=0 j=0 while(j 2020. 8. 15.
121. Best Time to Buy and Sell Stock class Solution: def maxProfit(self, prices: List[int]) -> int: if not prices: return 0 sell_prc = max(prices) max_prf = 0 for p in prices: if p < sell_prc: sell_prc = p # find the min value max_prf = max(p - sell_prc, max_prf) # compare current profit with max profit return max_prf 출처: leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/400573/Python3-beats-93.61-with-comments 2020. 8. 10.
1025. Divisor Game class Solution: def divisorGame(self, N: int) -> bool: dp = [False for i in range(N+1)] for i in range(N+1): for j in range(1, i//2 + 1): if i % j == 0 and (not dp[i - j]): dp[i] = True break return dp[N] Initialize solution as false till (N+1). N+1 because index 0 is ignored. Outer Loop (index i) from 1 to N (inclusive) and fill up the table till n. At the end, we will return dp[n] as answer In.. 2020. 8. 10.
856. Score of Parentheses leetcode.com/problems/score-of-parentheses/ class Solution: def scoreOfParentheses(self, S: str) -> int: if not S: return 0 count = 0 stack = [] flag = 0 for i in range(len(S)): if S[i] == "(": flag = 1 stack.append("(") if S[i] == ")": if flag == 1: count += 2**(len(stack)-1) flag = 0 stack.pop() return count 출처: leetcode.com/problems/score-of-parentheses/discuss/354283/Python-using-stack 2020. 8. 10.
856. Score of Parentheses leetcode.com/problems/score-of-parentheses/ class Solution: def scoreOfParentheses(self, S: str) -> int: if not S: return 0 count = 0 stack = [] flag = 0 for i in range(len(S)): if S[i] == "(": flag = 1 stack.append("(") if S[i] == ")": if flag == 1: count += 2**(len(stack)-1) flag = 0 stack.pop() return count 출처: leetcode.com/problems/score-of-parentheses/discuss/354283/Python-using-stack 2020. 8. 7.
1190. Reverse Substrings Between Each Pair of Parentheses class Solution: def reverseParentheses(self, s: str) -> str: #Take an empty stack for iteration stack = [] for i in range(0, len(s)): # If the current charector is anything other than closing bracket append it if s[i] != ')': stack.append(s[i]) else: k = [] while len(stack)!=0 and stack[-1]!='(': k.append(stack.pop()) # pop from stack and append: backward stack until opening bracket stack.pop() .. 2020. 8. 5.
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.