-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[Lyla] Week 1 #634
Changes from all commits
270473d
9b2784c
69a28ae
4a27dad
b4cb47c
de3351d
fcc3d6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)) | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (안 중요) 요렇게 좀 더 Pythonic 하게 코드를 작성하시면 다른 언어처럼 임시 변수를 안 쓰고 변수의 값을 스왑할 수 있을 것 같아요.
Suggested change
|
||||||||||
|
||||||||||
return prev1 |
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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 보통 이 문제는 다들 정렬이나 힙을 사용해서 많이 푸시는데, 배열을 사용하셔서 좀 더 효과적으로 해결하셨군요! |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😮 python은 이렇게 set 자료구조를 간편하게 쓸 수 있어 너무 깔끔하게 처리되는군요.