전체 글(115)
-
[Leetcode] 1456. Maximum Number of Vowels in a Substring of Given Length
🗓️ Daily LeetCoding Challenge May, Day 5 class Solution: def maxVowels(self, s: str, k: int) -> int: q = collections.deque(s[:k]) vowel = ['a', 'e', 'i', 'o', 'u'] count = 0 max_count = 0 for v in vowel: count += s[:k].count(v) max_count = max(max_count, count) i = k while i < len(s): if q.popleft() in vowel: count -= 1 x = s[i] q.append(x) if x in vowel: count += 1 max_count = max(max_count, co..
2023.05.06 -
[Leetcode] 649. Dota2 Senate
🗓️ Daily LeetCoding Challenge May, Day 4 class Solution: def predictPartyVictory(self, senate: str) -> str: A = collections.deque() people = [0, 0] bans = [0, 0] for person in senate: x = person == 'R' people[x] += 1 A.append(x) while all(people): x = A.popleft() people[x] -= 1 if bans[x]: bans[x] -= 1 else: people[x] +=1 bans[x^1] += 1 A.append(x) return 'Radiant' if people[1] else 'Dire'
2023.05.05 -
[Leetcode] 986. Interval List Intersections
class Solution: def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]: answer = [] i, j = 0, 0 if len(firstList) == 0 or len(secondList) == 0: return [] while i < len(firstList) and j < len(secondList): if firstList[i][1] < secondList[j][0]: i += 1 elif secondList[j][1] < firstList[i][0]: j += 1 else: intersection_start = max(firstList[i][0], ..
2023.05.05 -
[Leetcode] 844. Backspace String Compare
class Solution: def backspaceCompare(self, s: str, t: str) -> bool: def check(string): lst = [] for s in string: if s != "#": lst.append(s) else: if lst: lst.pop() return lst return check(s) == check(t)
2023.05.05 -
[Leetcode] 82. Remove Duplicates from Sorted List II
Solution 1. memoization class Solution: def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: memo = {} newhead = ListNode(0) cur = newhead while head: if head.val not in memo: memo[head.val] = 1 cur.next = ListNode(head.val) cur = cur.next else: memo[head.val] += 1 head = head.next lst = [k for k, v in memo.items() if v == 1] newhead = ListNode(0) cur = newhead for i in ls..
2023.05.04 -
[Leetcode] 2215. Find the Difference of Two Arrays
🗓️ Daily LeetCoding Challenge May, Day 3 class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: nums1 = set(nums1) nums2 = set(nums2) return nums1 - nums2, nums2 - nums1
2023.05.03