LeetCode

코-딩/Leetcode

[leetcode] 392. Is Subsequence(python)

문제 : https://leetcode.com/problems/is-subsequence/description/ 난이도 : Easy 문자열 s가 t에 포함되어 있는지 찾는 문제다. 어떻게 할까 고민하다가 그냥 찾으면 그 위치부터 자르고, 찾으면 자르고 단순하게 풀기로 했다. (늘 그랬음;;) 풀이1 class Solution: def isSubsequence(self, s: str, t: str) -> bool: answer = False tIdx, sIdx = 0, 0 if len(s) > len(t): return False if len(s) == 0: return True while len(t) > 0 and tIdx > -1: tIdx = t.find(s[sIdx]) if(tIdx > -1): #..

코-딩/Leetcode

[leetcode] 669. Trim a Binary Search Tree (python)

문제 : https://leetcode.com/problems/trim-a-binary-search-tree/ 난이도 : Medium 풀이 class Solution: def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]: def trim(node) -> Optional[TreeNode]: if node: if node.val > high: return trim(node.left) elif node.val < low: return trim(node.right) else: node.left = trim(node.left) node.right = trim(node.right) return node retur..

코-딩/Leetcode

[leetcode] 1422. Maximum Score After Splitting a String (python)

문제 : https://leetcode.com/problems/maximum-score-after-splitting-a-string/ 난이도 : Easy 풀이 1 class Solution: def maxScore(self, s: str) -> int: maxScore = 0 for i in range(1, len(s)): cnt = s[:i].count('0') + s[i:].count('1') maxScore = max(cnt, maxScore) return maxScore 단순하게 index를 기준으로 왼쪽은 0의 갯수를, 오른쪽은 1의 갯수를 세서 최댓값을 구하는 풀이이다. 풀이 2 class Solution: def maxScore(self, s: str) -> int: # 왼쪽 0의 갯수 + ..

코-딩/Leetcode

[leetcode] 74. Search a 2D Matrix (python)

문제 : https://leetcode.com/problems/search-a-2d-matrix/ 난이도 : Medium 풀이1 --- Brute Force class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for arr in matrix: if target bool: lo, hi = 0, len(matrix) - 1 while lo row[-1]: lo = midRow + 1 else: l, h = 0, len(row) - 1 while l row[mid]: l = mid + 1 else: h = mid - 1 return False '이미 정렬이 되어있는 배열에서 원소를 찾는다!' -> 이진트리를 ..

코-딩/Leetcode

[leetcode] 605. Can Place Flowers

문제: https://leetcode.com/problems/can-place-flowers/ 난이도: Easy 풀이 class Solution: def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: flowerbed.insert(0, 0) flowerbed.append(0) emptyCnt = 0 # 0이 연달아 3개면 심기 가능 flower = 0 # 심은 꽃 갯수 for fb in flowerbed: if fb == 0: emptyCnt += 1 else: emptyCnt = 0 # 리셋 if emptyCnt == 3: flower += 1 emptyCnt = 1 if flower == n: return True return False

코-딩/Leetcode

[leetcode] 714. Best Time to Buy and Sell Stock with Transaction Fee (python)

문제: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 난이도: Medium You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee. Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. N..

힞뚜루마뚜루
'LeetCode' 태그의 글 목록