Algorithm(218)
-
[Leetcode]22. Generate Parentheses(x)
1. problem :https://leetcode.com/problems/generate-parentheses/ 2. solution 1 :class Solution: def generateParenthesis(self, n: int) -> List[str]: result = [] def backTrack(s="",open_count = 0, close_count = 0): if len(s) == 2 * n: result.append(s) return if open_count backtracking을 활용하여 푸는 문제다. base case를 설정한 후, n..
2024.08.02 -
[Leetcode]150. Evaluate Reverse Polish Notation
1. problem : https://leetcode.com/problems/evaluate-reverse-polish-notation/ 2. solution 1 :from typing import Listclass Solution: def evalRPN(self, tokens: List[str]) -> int: stack = [] operations = { "+": lambda x, y: x + y, "-": lambda x, y: x - y, "*": lambda x, y: x * y, "/": lambda x, y: int(x / y) # 수정된 부분: 정수 나눗셈 } ..
2024.08.01 -
[Leetcode]155. Min Stack
1. problem : https://leetcode.com/problems/min-stack/ 2. solution 1 :class MinStack: def __init__(self): self.stack = [] self.min_stack = [] def push(self, val: int) -> None: self.stack.append(val) if not self.min_stack or val None: if self.stack: val = self.stack.pop() if val == self.min_stack[-1]: self.min_stack.pop..
2024.08.01 -
[Leetcode]20. Valid Parentheses
1. problem :https://leetcode.com/problems/valid-parentheses/ 2. solution 1 :class Solution: def isValid(self, s: str) -> bool: hashMap = {')':'(',']':'[','}':'{'} open_brackets = ['(','[','{'] temp_stack = [] for i in s: if i in open_brackets: temp_stack.append(i) else: if not temp_stack or hashMap[i] != temp_sta..
2024.08.01 -
[Leetcode]42. Trapping Rain Water(x)
1. problem : https://leetcode.com/problems/trapping-rain-water/description/ 2. solution 1 :class Solution: def trap(self, height: List[int]) -> int: if not height: return 0 n = len(height) left_max = [0] * n right_max = [0] * n left_max[0] = height[0] for i in range(1, n): left_max[i] = max(left_max[i - 1], height[i])..
2024.08.01 -
[Leetcode]167. Two Sum II - Input Array Is Sorted
1. problem : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 2. solution 1 :class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: hashMap = {} for i in range(len(numbers)): diff = target - numbers[i] if diff in hashMap.keys(): return [hashMap[diff],i+1] hashMap[numbers[i]] = i+1hashMap을 이..
2024.07.31