LeetCode 37

[LeetCode 75] Easy - 136. Single Number

136. Single Number class Solution: def singleNumber(self, nums: List[int]) -> int: # 2번 나온거는 무시하고, 1번 나온것만 찾음 # You must implement a solution with a linear runtime complexity and use only constant extra space. # 시간: O(n), 공간: O(1) 으로 풀어야 함 # for 루프 여러개? 단일 변수 여러개? # xor 하면(^) 2번 계산한건 0으로 사라짐 res = nums[0] for i in range(1, len(nums)): ..

LeetCode/Grind169 2024.11.09

[DP: LCS 최장공통수열 Longest Common Subsequence] 72. Edit Distance ★★★★★

72. Edit Distance https://youtu.be/XYi2-LPrwm4?si=8R5kcaUc9tWO8sSQ 초기값 세팅 설명초기 설정의 의미첫 번째 행과 열은 문자열이 비어 있을 때 작업의 기준점을 제공합니다.이후, 이 값을 기반으로 나머지 테이블 값을 채우며 두 문자열의 변환 비용을 계산합니다.이후 과정결론DP 테이블의 최종 값은 문자열을 변환하는 모든 가능한 경로를 고려해 최적의 변환 비용을 계산한 결과입니다. 이를 통해 삽입, 삭제, 교체 작업을 효율적으로 비교할 수 있습니다. Codeclass Solution: def minDistance(self, word1: str, word2: str) -> int: # DP 테이블 생성 # cache[i][..

714. Best Time to Buy and Sell Stock with Transaction Fee

714. Best Time to Buy and Sell Stock with Transaction Fee https://youtu.be/cUsPoH5DG1Q?si=vWqiiqYgUO8o6U1N Code class Solution: def maxProfit(self, prices: List[int], fee: int) -> int: n = len(prices) # buy[i]: i번째 날에 주식을 구매한 상태에서의 최대 이익 # sell[i]: i번째 날에 주식을 판매한 상태에서의 최대 이익 buy = [0] * n sell = [0] * n # 초기값: # 첫 번째 날에 주식을 구매한 경우: -prices[0] ..

LeetCode/DP심화 2024.11.08