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, count)
if count == k:
return k
i = i + 1
return max_count
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 34. Find First and Last Position of Element in Sorted Array (0) | 2023.05.09 |
---|---|
[Leetcode] 1498. Number of Subsequences That Satisfy the Given Sum Condition (1) | 2023.05.06 |
[Leetcode] 649. Dota2 Senate (0) | 2023.05.05 |
[Leetcode] 986. Interval List Intersections (0) | 2023.05.05 |
[Leetcode] 844. Backspace String Compare (0) | 2023.05.05 |
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'
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 1498. Number of Subsequences That Satisfy the Given Sum Condition (1) | 2023.05.06 |
---|---|
[Leetcode] 1456. Maximum Number of Vowels in a Substring of Given Length (0) | 2023.05.06 |
[Leetcode] 986. Interval List Intersections (0) | 2023.05.05 |
[Leetcode] 844. Backspace String Compare (0) | 2023.05.05 |
[Leetcode] 82. Remove Duplicates from Sorted List II (0) | 2023.05.04 |
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], secondList[j][0])
intersection_end = min(firstList[i][1], secondList[j][1])
answer.append([intersection_start, intersection_end])
if firstList[i][1] < secondList[j][1]:
i += 1
else:
j += 1
return answer
더 간단한 접근
class Solution:
def intervalIntersection(self, A, B):
i, j, ans = 0, 0, []
while i < len(A) and j < len(B):
curr = [max(A[i][0], B[j][0]), min(A[i][1], B[j][1])]
if curr[0] <= curr[1]:
ans.append(curr)
if A[i][1] <= B[j][1]:
i += 1
else:
j += 1
return ans
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 1456. Maximum Number of Vowels in a Substring of Given Length (0) | 2023.05.06 |
---|---|
[Leetcode] 649. Dota2 Senate (0) | 2023.05.05 |
[Leetcode] 844. Backspace String Compare (0) | 2023.05.05 |
[Leetcode] 82. Remove Duplicates from Sorted List II (0) | 2023.05.04 |
[Leetcode] 2215. Find the Difference of Two Arrays (0) | 2023.05.03 |
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)
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 649. Dota2 Senate (0) | 2023.05.05 |
---|---|
[Leetcode] 986. Interval List Intersections (0) | 2023.05.05 |
[Leetcode] 82. Remove Duplicates from Sorted List II (0) | 2023.05.04 |
[Leetcode] 2215. Find the Difference of Two Arrays (0) | 2023.05.03 |
[Leetcode] 1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2023.05.02 |
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 lst:
cur.next = ListNode(i)
cur = cur.next
return newhead.next
Solution 2. Two Pointer (플로이드 사이클 알고리즘 응용)
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0)
dummy.next = head
fast, slow = head, dummy
# slow는 기록이 완료된 노드
# fast는 탐색을 진행하는 노드
while fast:
while fast.next and fast.val == fast.next.val:
fast = fast.next
if slow.next == fast:
slow = slow.next
fast = fast.next
else:
slow.next = fast.next
fast = fast.next
return dummy.next
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 986. Interval List Intersections (0) | 2023.05.05 |
---|---|
[Leetcode] 844. Backspace String Compare (0) | 2023.05.05 |
[Leetcode] 2215. Find the Difference of Two Arrays (0) | 2023.05.03 |
[Leetcode] 1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2023.05.02 |
[Leetcode] 1822. Sign of the Product of an Array (0) | 2023.05.02 |
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
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 844. Backspace String Compare (0) | 2023.05.05 |
---|---|
[Leetcode] 82. Remove Duplicates from Sorted List II (0) | 2023.05.04 |
[Leetcode] 1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2023.05.02 |
[Leetcode] 1822. Sign of the Product of an Array (0) | 2023.05.02 |
[Leetcode] 121. Best Time to Buy and Sell Stock (0) | 2023.05.02 |
import pytest
import tensorflow as tf
from PIL import Image
import utils
@pytest.fixture
def pil_image():
return Image.new('RGB', (32, 32), color='red')
def test_convert_pil_to_tensor(pil_image):
tensor = utils.convert_pil_to_tensor(pil_image)
assert tensor.shape == (32, 32, 3)
assert tensor.dtype == tf.float32
@pytest.fixture의 역할
Dummy 테스트 환경을 구성
위의 예시에서는 PIL.Image 객체를 생성하여 test_conver_pil_to_tensor 함수에 넘겨줌
@pytest.fixture의 흐름
테스트 함수에서 parameter로 fixture 함수를 명시
이후 pytest에서 자동으로 fixture 함수를 호출하여 return을 input parameter로 넘겨줌
fixture 함수를 사용함으로써 테스트를 위해 간단한 dummy 데이터 생성 가능
'Programming Language > Python' 카테고리의 다른 글
게으른 로깅 (0) | 2023.07.29 |
---|---|
logging 사용법과 관련 이슈 (0) | 2023.03.27 |
🗓️ Daily LeetCoding Challenge May, Day 1
class Solution:
def average(self, salary: List[int]) -> float:
return (sum(salary) - min(salary) - max(salary)) / (len(salary) - 2)
'CS > 알고리즘' 카테고리의 다른 글
[Leetcode] 82. Remove Duplicates from Sorted List II (0) | 2023.05.04 |
---|---|
[Leetcode] 2215. Find the Difference of Two Arrays (0) | 2023.05.03 |
[Leetcode] 1822. Sign of the Product of an Array (0) | 2023.05.02 |
[Leetcode] 121. Best Time to Buy and Sell Stock (0) | 2023.05.02 |
[Leetcode 334] 334. Increasing Triplet Subsequence (0) | 2023.05.02 |