Skip to content

Commit 97dee73

Browse files
authored
Merge pull request #1218 from yayyz/main
[yayyz] WEEK 02 Solutions
2 parents 5a72122 + 0b0a799 commit 97dee73

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

โ€Ž3sum/yayyz.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort()
4+
result = []
5+
for i in range(len(nums)):
6+
if i > 0 and nums[i] == nums[i - 1]:
7+
continue
8+
9+
left = i + 1
10+
right = len(nums) - 1
11+
while left < right:
12+
total = nums[i] + nums[left] + nums[right]
13+
if total < 0:
14+
left += 1
15+
elif total > 0:
16+
right -= 1
17+
else:
18+
result.append([nums[i], nums[left], nums[right]])
19+
left += 1
20+
right -= 1
21+
while left < right and nums[left] == nums[left -1]:
22+
left += 1
23+
while left < right and nums[right] == nums[right + 1]:
24+
right -= 1
25+
return result

โ€Žclimbing-stairs/yayyz.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
๐Ÿ“ Problem: LeetCode 70 - Climbing Stairs
3+
๐Ÿ“… Date: 2025-04-07
4+
5+
๐Ÿš€ Approach:
6+
- Bottom-up DP using an array
7+
- dp[i] = dp[i-1] + dp[i-2]
8+
9+
โฑ๏ธ Time Complexity: O(n)
10+
๐Ÿ’พ Space Complexity: O(n)
11+
12+
๐Ÿ“Œ Notes:
13+
- Base case: dp[0] = 1, dp[1] = 1
14+
- dp[i]: i๋ฒˆ์งธ ๊ณ„๋‹จ์œผ๋กœ ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฐ€์ง
15+
- n <= 2์˜ ๊ฒฝ์šฐ๋Š” f(1) + f(0)์ด ํ•ฉํ•ด์ง„ ๊ฒฝ์šฐ์ด๊ธฐ ๋•Œ๋ฌธ์— n์„ ๋ฐ˜ํ™˜
16+
"""
17+
class Solution:
18+
def climbStairs(self, n: int) -> int:
19+
if n <= 2:
20+
return n
21+
dp = [0] * (n + 1) # n ๋ฒˆ์งธ์˜ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๊ธฐ ์œ„ํ•ด dp๋ฐฐ์—ด ์ƒ์„ฑ
22+
dp[0] = 1
23+
dp[1] = 1
24+
# n๋ฒˆ์งธ์˜ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๊ธฐ ์œ„ํ•ด์„œ๋Š”
25+
# n-1, n-2๋ฒˆ์งธ์˜ ๊ณ„๋‹จ์—์„œ ์˜ฌ์ˆ˜์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋“ค์˜ ํ•ฉ์ด n๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๊ธฐ ์œ„ํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜
26+
for i in range(2, n + 1):
27+
dp[i] = dp[i-1] + dp[i-2]
28+
return dp[n]

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Solution 1: using Counter, heapq
2+
from collections import Counter
3+
import heapq
4+
5+
class Solution:
6+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
7+
count_dict = Counter(nums)
8+
return heapq.nlargest(k, count_dict.keys(), key=count_dict.get)
9+
10+
# Solution 2: create dict, use sorted function
11+
# class Solution:
12+
# def topKFrequent(self, nums: List[int], k: int) -> List[int]:
13+
# freq_dict = {}
14+
# for num in nums:
15+
# if num in freq_dict:
16+
# freq_dict[num] += 1
17+
# else:
18+
# freq_dict[num] = 1
19+
# sorted_list = sorted(freq_dict.keys(), key = lambda x: freq_dict[x], reverse=True)
20+
# return sorted_list[:k]

โ€Žvalid-anagram/yayyz.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
return Counter(s) == Counter(t)

0 commit comments

Comments
ย (0)