본문 바로가기

전체 글267

303. Range Sum Query - Immutable class NumArray(object): def __init__(self, nums): """ initialize your data structure here. :type nums: List[int] """ self.sums = [0] * (len(nums) + 1) for i in xrange(len(nums)): self.sums[i+1] = self.sums[i] + nums[i] def sumRange(self, i, j): """ sum of elements nums[i..j], inclusive. :type i: int :type j: int :rtype: int """ return self.sums[j+1] - self.sums[i] 출처: leetcode.com/problems/range.. 2020. 8. 16.
53. Maximum Subarray class Solution: # @param A, a list of integers # @return an integer # 6:57 def maxSubArray(self, A): if not A: return 0 curSum = maxSum = A[0] for num in A[1:]: curSum = max(num, curSum + num) # max(current index value, current max sub array w/ current idx value) maxSum = max(maxSum, curSum) # get each sub array's max sub array return maxSum www.youtube.com/watch?time_continue=487&v=2MmGzdiKR9Y&.. 2020. 8. 16.
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.