Skip to content

[Lyla] Week 1 #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions contains-duplicate/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮 python은 이렇게 set 자료구조를 간편하게 쓸 수 있어 너무 깔끔하게 처리되는군요.

16 changes: 16 additions & 0 deletions house-robber/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List

class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]

# prev1: 이전 집까지의 최대 이익
# prev2: 전전 집까지의 최대 이익
prev1, prev2 = 0, 0
for num in nums:
temp = prev1
prev1 = max(prev2 + num, prev1) # 현재 집을 털었을 때와 안 털었을 때 중 더 큰 이익 선택
prev2 = temp
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(안 중요) 요렇게 좀 더 Pythonic 하게 코드를 작성하시면 다른 언어처럼 임시 변수를 안 쓰고 변수의 값을 스왑할 수 있을 것 같아요.

Suggested change
temp = prev1
prev1 = max(prev2 + num, prev1) # 현재 집을 털었을 때와 안 털었을 때 중 더 큰 이익 선택
prev2 = temp
prev1, prev2 = max(prev2 + num, prev1), prev1


return prev1
22 changes: 22 additions & 0 deletions longest-consecutive-sequence/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
# Convert to set for O(1) lookups
num_set = set(nums)
longest_length = 0

for num in num_set:
# Only start counting if num is the start of a sequence
if num - 1 not in num_set:
current_num = num
current_length = 1

# Count the length of the sequence
while current_num + 1 in num_set:
current_num += 1
current_length += 1

longest_length = max(longest_length, current_length)

return longest_length
24 changes: 24 additions & 0 deletions top-k-frequent-elements/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from collections import Counter
from typing import List

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# 빈도 계산
count = Counter(nums)
n = len(nums)

# 빈도수를 기준으로 버킷 생성 (0에서 n까지)
buckets = [[] for _ in range(n + 1)]

# 각 숫자를 해당 빈도수의 버킷에 추가
for num, freq in count.items():
buckets[freq].append(num)

# 빈도가 높은 순서대로 k개의 숫자를 추출
result = []
for freq in range(n, 0, -1):
if buckets[freq]:
result.extend(buckets[freq])

if len(result) == k:
return result
Comment on lines +17 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

보통 이 문제는 다들 정렬이나 힙을 사용해서 많이 푸시는데, 배열을 사용하셔서 좀 더 효과적으로 해결하셨군요!
result 리스트 안에 원소가 k개가 되자 마자 바로 반환하는 최적화 센스도 너무 좋으세요! 👍
알고리즘 고수의 향기를 맡고 갑니다 🌸

21 changes: 21 additions & 0 deletions valid-palindrome/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
# two pointer
left, right = 0, len(s) - 1

while left < right:
# compare only alphanumeric characters
while left < right and not s[left].isalnum():
left += 1
while left < right and not s[right].isalnum():
right -= 1

# compare with lowercase
if s[left].lower() != s[right].lower():
return False

# move pointers
left += 1
right -= 1

return True
Loading