Skip to content

[yayyz] WEEK 02 Solutions #1218

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
Apr 14, 2025
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
25 changes: 25 additions & 0 deletions 3sum/yayyz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
result = []
for i in range(len(nums)):
if i > 0 and nums[i] == nums[i - 1]:
continue

left = i + 1
right = len(nums) - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total < 0:
left += 1
elif total > 0:
right -= 1
else:
result.append([nums[i], nums[left], nums[right]])
left += 1
right -= 1
while left < right and nums[left] == nums[left -1]:
left += 1
while left < right and nums[right] == nums[right + 1]:
right -= 1
return result
28 changes: 28 additions & 0 deletions climbing-stairs/yayyz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
📝 Problem: LeetCode 70 - Climbing Stairs
📅 Date: 2025-04-07

🚀 Approach:
- Bottom-up DP using an array
- dp[i] = dp[i-1] + dp[i-2]

⏱️ Time Complexity: O(n)
💾 Space Complexity: O(n)

📌 Notes:
- Base case: dp[0] = 1, dp[1] = 1
- dp[i]: i번째 계단으로 도달하기 위한 모든 경우의 수를 가짐
- n <= 2의 경우는 f(1) + f(0)이 합해진 경우이기 때문에 n을 반환
"""
class Solution:
def climbStairs(self, n: int) -> int:
if n <= 2:
return n
dp = [0] * (n + 1) # n 번째의 계단을 오르는 방법을 찾기 위해 dp배열 생성
dp[0] = 1
dp[1] = 1
# n번째의 계단을 오르기 위해서는
# n-1, n-2번째의 계단에서 올수있는 경우의 수들의 합이 n번째 계단을 오르기 위한 모든 경우의 수
for i in range(2, n + 1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]
20 changes: 20 additions & 0 deletions top-k-frequent-elements/yayyz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Solution 1: using Counter, heapq
from collections import Counter
import heapq

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
count_dict = Counter(nums)
return heapq.nlargest(k, count_dict.keys(), key=count_dict.get)

# Solution 2: create dict, use sorted function
# class Solution:
# def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# freq_dict = {}
# for num in nums:
# if num in freq_dict:
# freq_dict[num] += 1
# else:
# freq_dict[num] = 1
# sorted_list = sorted(freq_dict.keys(), key = lambda x: freq_dict[x], reverse=True)
# return sorted_list[:k]
3 changes: 3 additions & 0 deletions valid-anagram/yayyz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
return Counter(s) == Counter(t)