-
-
Notifications
You must be signed in to change notification settings - Fork 195
[yeoju] Week 2 #755
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
[yeoju] Week 2 #755
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,27 @@ | ||
# 전체 시간복잡도 : O(n^2), 공간복잡도 : O(n) | ||
class Solution: | ||
def threeSum(self, nums: list[int]) -> list[list[int]]: | ||
nums.sort() # 내장함수 sort()의 시간복잡도 O(nlogn) | ||
result = [] # 공간복잡도 O(n) | ||
# 시간복잡도 O(n) | ||
for i in range(len(nums) - 2): | ||
if i > 0 and nums[i] == nums[i - 1]: | ||
continue | ||
r = len(nums) - 1 | ||
l = i + 1 | ||
# i를 기준으로 l과 r을 탐색하는 시간 복잡도 : O(n) | ||
while l < r: | ||
if nums[i] + nums[l] + nums[r] < 0: | ||
l += 1 | ||
elif nums[i] + nums[l] + nums[r] > 0: | ||
r -= 1 | ||
else: | ||
result.append([nums[i], nums[l], nums[r]]) | ||
while l < r and nums[l] == nums[l + 1]: # 중복 제거 반복문, 이미 진행된 리스트를 다시 탐색하지 않으므로 시간복잡도는 추가되지 않음 | ||
l += 1 | ||
while l < r and nums[r] == nums[r - 1]: | ||
r -= 1 | ||
l += 1 | ||
r -= 1 | ||
return result | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 시간복잡도 : O(n), 공간복잡도 : O(1) | ||
|
||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
a = 1 | ||
b = 2 | ||
if (n == 1): | ||
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 1 | ||
elif (n == 2): | ||
return 2 | ||
for i in range(n - 2): | ||
c = a | ||
a = b | ||
b = c + b | ||
return (b) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 시간 복잡도 : O(n), 공간 복잡도 : O(n) | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
tmp = {} | ||
# tmp에 char:횟수 형식으로 저장 | ||
for key in s: | ||
if key in tmp: | ||
tmp[key] += 1 | ||
else: | ||
tmp[key] = 1 | ||
Comment on lines
+7
to
+10
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. 파이썬에서 딕셔너리에 키가 없으면 기본값을 대입하고 싶은 경우, default dict를 사용하거나 .get 메서드의 기본값 옵션을 사용할 수 있습니다. 이러면 조건문을 줄이고 가독성이 좋아질 것 같아요. |
||
# t 문자열을 돌면서 나타나는 char에 대한 tmp의 횟수 차감 | ||
# tmp에 존재하지 않는 char 발생 시 return False | ||
for key in t: | ||
if key in tmp: | ||
tmp[key] -= 1 | ||
else: | ||
return False | ||
# tmp를 돌면서 value값이 모두 0인지 확인, 모두 0일 경우 s와 t는 anagram이다 | ||
for i in tmp: | ||
if (tmp[i] != 0): | ||
return False | ||
return True | ||
|
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.
파이썬이니까 가능하면 PEP 8에 맞춰서 인덴트를 4 space로 설정해주시면 좋아요