코-딩/Leetcode

코-딩/Leetcode

[leetcode] 997. Find the Town Judge

문제 : https://leetcode.com/problems/find-the-town-judge/description/ 난이도 : Lv.1 풀이 class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if not trust: if n == 1: return 1 else: return -1 relationship = [[] for _ in range(n+1)] for a, b in trust: #a는 b를 신뢰 relationship[b].append(a) for i in range(1, n+1): if len(relationship[i]) == n-1: for t in range(1, n+1): if i in relatio..

코-딩/Leetcode

[leetcode] 14.Longest Common Prefix

문제 : https://leetcode.com/problems/longest-common-prefix/description/ 난이도 : Easy 풀이 class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs) == 1: return strs[0] strs = sorted(strs) minLen = min(len(strs[0]), len(strs[-1])) i = 0 while i < minLen and strs[0][i] == strs[-1][i]: i += 1 prefix = strs[0][0:i] return prefix 배열을 다 훑어서 하나하나 비교해야 될 것 같지만, 사실은 오름차순 정렬 후 처음과 끝의 ..

코-딩/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] 338. Counting Bits

문제 : https://leetcode.com/problems/counting-bits/ 난이도 : Easy 풀이 class Solution: def countBits(self, n: int) -> List[int]: arr = [] for i in range(n + 1): arr.append(bin(i).count('1')) return arr 파이썬에는 함수가 많다. 최고야 1) bin() 2) count()

힞뚜루마뚜루
'코-딩/Leetcode' 카테고리의 글 목록 (3 Page)