Skip to content

Commit 8ed7437

Browse files
authored
Merge pull request #634 from pmjuu/main
[Lyla] Week 1
2 parents 67949d5 + fcc3d6e commit 8ed7437

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

โ€Žcontains-duplicate/pmjuu.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
return len(nums) != len(set(nums))

โ€Žhouse-robber/pmjuu.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
class Solution:
4+
def rob(self, nums: List[int]) -> int:
5+
if len(nums) == 1:
6+
return nums[0]
7+
8+
# prev1: ์ด์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ์ด์ต
9+
# prev2: ์ „์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ์ด์ต
10+
prev1, prev2 = 0, 0
11+
for num in nums:
12+
temp = prev1
13+
prev1 = max(prev2 + num, prev1) # ํ˜„์žฌ ์ง‘์„ ํ„ธ์—ˆ์„ ๋•Œ์™€ ์•ˆ ํ„ธ์—ˆ์„ ๋•Œ ์ค‘ ๋” ํฐ ์ด์ต ์„ ํƒ
14+
prev2 = temp
15+
16+
return prev1
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
# Convert to set for O(1) lookups
6+
num_set = set(nums)
7+
longest_length = 0
8+
9+
for num in num_set:
10+
# Only start counting if num is the start of a sequence
11+
if num - 1 not in num_set:
12+
current_num = num
13+
current_length = 1
14+
15+
# Count the length of the sequence
16+
while current_num + 1 in num_set:
17+
current_num += 1
18+
current_length += 1
19+
20+
longest_length = max(longest_length, current_length)
21+
22+
return longest_length

โ€Žtop-k-frequent-elements/pmjuu.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import Counter
2+
from typing import List
3+
4+
class Solution:
5+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
6+
# ๋นˆ๋„ ๊ณ„์‚ฐ
7+
count = Counter(nums)
8+
n = len(nums)
9+
10+
# ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฒ„ํ‚ท ์ƒ์„ฑ (0์—์„œ n๊นŒ์ง€)
11+
buckets = [[] for _ in range(n + 1)]
12+
13+
# ๊ฐ ์ˆซ์ž๋ฅผ ํ•ด๋‹น ๋นˆ๋„์ˆ˜์˜ ๋ฒ„ํ‚ท์— ์ถ”๊ฐ€
14+
for num, freq in count.items():
15+
buckets[freq].append(num)
16+
17+
# ๋นˆ๋„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ k๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ถ”์ถœ
18+
result = []
19+
for freq in range(n, 0, -1):
20+
if buckets[freq]:
21+
result.extend(buckets[freq])
22+
23+
if len(result) == k:
24+
return result

โ€Žvalid-palindrome/pmjuu.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
# two pointer
4+
left, right = 0, len(s) - 1
5+
6+
while left < right:
7+
# compare only alphanumeric characters
8+
while left < right and not s[left].isalnum():
9+
left += 1
10+
while left < right and not s[right].isalnum():
11+
right -= 1
12+
13+
# compare with lowercase
14+
if s[left].lower() != s[right].lower():
15+
return False
16+
17+
# move pointers
18+
left += 1
19+
right -= 1
20+
21+
return True

0 commit comments

Comments
ย (0)