전체 글(334)
-
[Leetcode]704. Binary Search
1. problem : https://leetcode.com/problems/binary-search/ 2. solution 1 :class Solution: def search(self, nums: List[int], target: int) -> int: left,right = 0 , len(nums) -1 while left target: right = mid - 1 else: return mid return -1
2024.08.02 -
[Leetcode]84. Largest Rectangle in Histogram(x)
1. problem : https://leetcode.com/problems/largest-rectangle-in-histogram/description/ 2. solution 1 :class Solution: def largestRectangleArea(self,heights : List[int]) -> int: stack = [] index = 0 max_area = 0 while index 상당히 어렵다. stack을 쓰는 문제.. 외우자.
2024.08.02 -
[Leetcode]853. Car Fleet(x)
1. problem : https://leetcode.com/problems/car-fleet/ 2. solution 1 :class Solution: def carFleet(self, target: int, position: List[int], speed: List[int]) -> int: cars = sorted(zip(position,speed),reverse=True) times = [(target - p)/s for p,s in cars] current_time = 0 fleet = 0 for time in times: if time > current_time: fleet +=1 ..
2024.08.02 -
[Leetcode]739. Daily Temperatures(x)
1. problem : https://leetcode.com/problems/daily-temperatures/ 2. solution 1 :class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: ans = [0] * len(temperatures) stack = [] for i,temp in enumerate(temperatures): while stack and temperatures[stack[-1]]
2024.08.02 -
[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