본문 바로가기

분류 전체보기

(174)
[Leetcode] 621. Task Scheduler https://leetcode.com/problems/task-scheduler class Solution: def leastInterval(self, tasks: List[str], n: int) -> int: frequency = [0] * 26 for t in tasks: frequency[ord(t) - ord('A')] += 1 max_frequency_count = frequency.count(max(frequency)) shortest = len(tasks) longest = (max(frequency) - 1) * (n + 1) + max_frequency_count return max(shortest, longest)
[Leetcode] 452. Minimum Number of Arrows to Burst Balloons https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons class Solution: def findMinArrowShots(self, points: List[List[int]]) -> int: if len(points) == 0: return 0 points.sort(key = lambda x: x[1]) count = 1 end = points[0][1] for i in range(1, len(points)): if points[i][0] > end: count += 1 end = points[i][1] return count
[Leetcode] 57. Insert Interval https://leetcode.com/problems/insert-interval/ class Solution: def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: new = [] for i in range(len(intervals)): if newInterval[1]intervals[i][1]: new.append(intervals[i]) else: newInterval=[min(newInterval[0],intervals[i][0]),max(intervals[i][1],newInterval[1])] new.append(newInterval) return new
[Leetcode] 3083. Existence of a Substring in a String and Its Reverse https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse class Solution: def isSubstringPresent(self, s: str) -> bool: r = s[::-1] substrs = [] for i in range(0, len(s)-1): substrs.append(s[i:i+2]) for s in substrs: if s in r: return True return False
[Leetcode] 3079. Find the Sum of Encrypted Integers https://leetcode.com/problems/find-the-sum-of-encrypted-integers/ class Solution: def sumOfEncryptedInt(self, nums: List[int]) -> int: encrypted_nums = [] for n in nums: ndigit = list(map(int, str(n))) if len(ndigit) > 1: max = -1 for i in ndigit: if max < i: max = i encrypted = int(str(max) * len(ndigit)) encrypted_nums.append(encrypted) else: encrypted_nums.append(n) sum = 0 for n in encrypted..
[Leetcode] 525. Contiguous Array https://leetcode.com/problems/contiguous-array class Solution: def findMaxLength(self, nums: List[int]) -> int: length = 0 count = 0 count_map = {0: -1} for i in range(len(nums)): if nums[i] == 0: count -= 1 else: count += 1 if count in count_map: length = max(length, i - count_map[count]) else: count_map[count] = i return length
[Leetcode] 238. Product of Array Except Self https://leetcode.com/problems/product-of-array-except-self class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: result = [1] * len(nums) prifix_value = 1 for i in range(0, len(nums)): result[i] *= prifix_value prifix_value *= nums[i] suffix_value = 1 for i in range(len(nums)-1, -1, -1): result[i] *= suffix_value suffix_value *= nums[i] return result
[Leetcode] 930. Binary Subarrays With Sum https://leetcode.com/problems/binary-subarrays-with-sum class Solution: def numSubarraysWithSum(self, nums: List[int], goal: int) -> int: count = 0 prifix = {0:1} sum = 0 for i in nums: sum += i count += prifix.get(sum - goal, 0) prifix[sum] = prifix.get(sum, 0) + 1 return count

728x90