본문 바로가기

전체 글

(113)
398. Random Pick Index leetcode.com/problems/random-pick-index/ Random Pick Index - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def __init__(self, nums: List[int]): self.nums = nums def pick(self, target: int) -> int: output = 0 count = 0 for i, x in enumerate(self.nums) : if x != tar..
388. Longest Absolute File Path leetcode.com/problems/longest-absolute-file-path/ Longest Absolute File Path - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def lengthLongestPath(self, input: str) -> int: tokens = input.split('\n') if not tokens : return 0 d = collections.defaultdict(int) output..
914. X of a Kind in a Deck of Cards class Solution: def hasGroupsSizeX(self, deck: List[int]) -> bool: if len(deck) < 2 : return False m = collections.defaultdict(int) for i in deck : m[i] += 1 psize = m[deck[0]] for i in m.values() : psize = self.gcd(psize, i) if psize == 1 : return False return True def gcd(self, a, b) : while b != 0 : n = a % b a = b b = n return a 정리 : 최대 공약수 활용. # 최대공약수 (Greatest Common Divisor)를 구하는 가장 쉬운 방법..
41. First Missing Positive leetcode.com/problems/first-missing-positive/ First Missing Positive - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def firstMissingPositive(self, nums: List[int]) -> int: if 1 not in nums : return 1 n = len(nums) for i in range(n) : if nums[i] n : nums[i] = 1 fo..
134. Gas Station leetcode.com/problems/gas-station/ Gas Station - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: n = len(gas) total = current = 0 start = 0 for i in range(n) : amount = gas[i] - cost[i] total += a..
416. Partition Equal Subset Sum leetcode.com/problems/partition-equal-subset-sum/ Partition Equal Subset Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com # Brute Force class Solution: def canPartition(self, nums: List[int]) -> bool: total = sum(nums) if total % 2 : return False self.memo = {} return self.dfs..
784. Letter Case Permutation leetcode.com/problems/letter-case-permutation/ Letter Case Permutation - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def letterCasePermutation(self, S: str) -> List[str]: output = [""] for i in range(len(S)) : curr = [] for j in range(len(output)) : if S[i].isal..
Greedy algorithm - An algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. So the problems where choosing locally optimal also leads to global solution are best fit for Greedy. - Fractional Knapsack Problem, the local optimal strategy is to choose the item that has maximum value vs weight ratio. This strategy also leads ..