코-딩/Leetcode

코-딩/Leetcode

[leetcode] 720. Longest Word in Dictionary (python)

문제 : https://leetcode.com/problems/longest-word-in-dictionary/ 난이도 : Medium 풀이 class Solution: def longestWord(self, words: List[str]) -> str: lWord = [''] for word in sorted(words): if word[:-1] in lWord: lWord.append(word) lWord = sorted(lWord, key=len, reverse=True) return lWord[0] 단어들을 사전순으로 정렬한 뒤, lWord에 마지막 글자를 제외한 단어가 존재한다면 lWord에 푸쉬 길이 순대로 정렬한 뒤 제일 앞에 있는 단어 return

코-딩/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

[leetcode]242. Valid Anagram (python)

문제: https://leetcode.com/problems/valid-anagram/ 난이도: easy Anagaram 개념만 알면 쉽게 생각해서 풀 수 있는 문제다. 풀이 class Solution: def isAnagram(self, s: str, t: str) -> bool: if s is None or t is None: return False if len(s) != len(t): return False if sorted(s) == sorted(t): return True

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