-
-
Notifications
You must be signed in to change notification settings - Fork 248
[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
Changes from all commits
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,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 | ||
left += 1 | ||
right -= 1 | ||
|
||
return answer |
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] | ||
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. DP와 예외처리를 이용해서 깔끔하게 풀어주셨네요! 추가로 |
||
|
||
return dp[n] |
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): | ||
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.
|
||
return True | ||
dic[num] = 1 | ||
|
||
return False |
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 |
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) |
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.
nums
를 정렬한 후 투 포인터 이동 시 중복된 원소는 건너뛰는 방식으로 최적화를 하신 것 같네요 🤩