Skip to content

[chordpli] WEEK 02 solutions #1776

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 2 commits into from
Aug 3, 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
31 changes: 31 additions & 0 deletions 3sum/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import List


class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
answer = []
nums.sort()
nums_len = len(nums)

for i in range(nums_len - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
left, right = i + 1, nums_len - 1

while left < right:
current_sum = nums[i] + nums[left] + nums[right]
if current_sum < 0:
left += 1
elif current_sum > 0:
right -= 1

else:
answer.append([nums[i], nums[left], nums[right]])
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
Copy link
Contributor

Choose a reason for hiding this comment

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

nums를 정렬한 후 투 포인터 이동 시 중복된 원소는 건너뛰는 방식으로 최적화를 하신 것 같네요 🤩

left += 1
right -= 1

return answer
13 changes: 13 additions & 0 deletions climbing-stairs/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def climbStairs(self, n: int) -> int:
if n <= 2:
return n

dp = [0] * (n + 1)
dp[1] = 1
dp[2] = 2

for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
Copy link
Contributor

Choose a reason for hiding this comment

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

DP와 예외처리를 이용해서 깔끔하게 풀어주셨네요!

추가로 dp[i]를 계산할 때 dp[i - 1]dp[i - 2]만 사용하기 때문에, O(n) space의 리스트가 아닌 O(1) space의 변수 두 개를 이용해서 공간 복잡도를 더 최적화 할 수도 있을 것 같습니다!


return dp[n]
12 changes: 12 additions & 0 deletions contains-duplicate/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
dic = {}

for num in nums:
if dic.get(num):
Copy link
Contributor

Choose a reason for hiding this comment

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

numdic에 존재하는지만 O(1)에 확인하려면 dict()가 아닌 set()을 사용할 수도 있을 것 같아요!

return True
dic[num] = 1

return False
11 changes: 11 additions & 0 deletions two-sum/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import List

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in map:
return [map[complement], i]

map[num] = i
19 changes: 19 additions & 0 deletions valid-anagram/chordpli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
# dictionary 사용 11ms
def isAnagram(self, s: str, t: str) -> bool:
check = {}
for char in s:
if char not in check:
check[char] = 0
check[char] += 1
for char in t:
if char not in check:
return False
check[char] -= 1
if check[char] == 0:
del check[char]
return not check

# 정렬 사용 15 - 20ms
# def isAnagram(self, s: str, t: str) -> bool:
# return sorted(s) == sorted(t)