-
-
Notifications
You must be signed in to change notification settings - Fork 248
[ayleeee] WEEK 02 Solutions #1253
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e121646
valid-anagram solution
ayleeee b2fb0ee
Merge branch 'main' of https://github.com/ayleeee/leetcode-study
ayleeee bfe7963
Merge branch 'DaleStudy:main' into main
ayleeee 8618ef3
solved : climbing-stairs
ayleeee e02ef09
Update ayleeee.py
ayleeee 9e6582b
Update ayleeee.py
ayleeee 8f0f654
solved : proudct-of-array-except-self
ayleeee bd601c6
solved : 3sum
ayleeee c719829
solved : validate-binary-search-tree
ayleeee b144d2b
Update ayleeee.py
ayleeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# 투포인터를 활용해보기 | ||
|
||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
n = len(nums) | ||
nums.sort() | ||
result = set() | ||
for i in range(n): | ||
l,r = i+1,n-1 | ||
while l<r: | ||
res = nums[i] + nums[l] + nums[r] | ||
if res < 0: | ||
l += 1 | ||
elif res > 0: | ||
r -= 1 | ||
else: | ||
result.add((nums[i], nums[l], nums[r])) | ||
l += 1 | ||
return list(result) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# takes n steps to reach the top | ||
# 1 or 2 steps at a time | ||
|
||
''' | ||
n = 1 | ||
answer = 1 | ||
|
||
n = 2 | ||
answer = 2 | ||
1 step + 1 step | ||
2 step | ||
|
||
n = 3 | ||
answer = 3 | ||
1 step + 1 step + 1 step | ||
1 step + 2 step | ||
2 step + 1 step | ||
|
||
n=4 | ||
answer = 5 | ||
1 step + 1 step + 1 step + 1 step | ||
1 step + 1 step + 2 step | ||
1 step + 2 step + 1 step | ||
2 step + 1 step + 1 step | ||
2 step + 2 step | ||
|
||
n=5 | ||
answer = 8 | ||
1 + 1 + 1 + 1 + 1 | ||
1 + 1 + 1 + 2 | ||
1 + 1 + 2 + 1 | ||
1 + 2 + 1 + 1 | ||
1 + 2 + 2 | ||
2 + 1 + 1 + 1 | ||
2 + 1 + 2 | ||
2 + 2 + 1 | ||
|
||
1 2 3 5 8 ? ? | ||
''' | ||
|
||
# 시간 복잡도 O(n) | ||
# 공간 복잡도 O(n) | ||
def climbStairs(self, n: int) -> int: | ||
dp = [0] * (n + 1) | ||
if n == 1: | ||
return 1 | ||
elif n == 2: | ||
return 2 | ||
dp[1] = 1 | ||
dp[2] = 2 | ||
for i in range(3, n + 1): | ||
dp[i] = dp[i - 1] + dp[i - 2] | ||
return dp[n] | ||
|
||
'''def climbStairs(self, n: int) -> int: | ||
if n == 1: | ||
return 1 | ||
prev1, prev2 = 1, 2 | ||
for i in range(3, n + 1): | ||
current = prev1 + prev2 | ||
prev1 = prev2 | ||
prev2 = current | ||
return prev2 | ||
시간 복잡도 : O(n) -> 루프가 있기에 | ||
공간 복잡도 : O(1) -> 추가 배열 없이 상수 공간만 사용 | ||
|
||
''' | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 정수 배열 nums가 주어졌을 때, 각 위치 i에 대해 | ||
# nums[i]를 제외한 나머지 모든 요소의 곱을 반환하는 함수를 작성 | ||
|
||
''' | ||
nums = [1, 2, 3, 4] | ||
answers = [24, 12, 8, 6] | ||
answers[0] = 2 * 3 * 4 | ||
answers[1] = 1 * 3 * 4 | ||
answers[2] = 1 * 2 * 4 | ||
answers[3] = 1 * 2 * 3 | ||
|
||
조건 : O(n) 시간 복잡도 | ||
|
||
왼쪽에서 오른쪽으로 누적 곱 계산 | ||
오른쪽에서 왼쪽으로 순회하면서, 오른쪽 누적 곱을 계산 | ||
왼쪽 누적 곱과 곱하여 최종 결과 업데이트 | ||
|
||
''' | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
n = len(nums) | ||
result = [1] * n | ||
left = 1 | ||
right = 1 | ||
# 왼쪽에서 오른쪽으로 누적 곱 계산 | ||
for i in range(n): | ||
result[i] = left | ||
left *= nums[i] | ||
# 오른쪽에서 왼쪽으로 누적 곱 계산 | ||
# 역순으로 반복하는 구문, 오른쪽에서 왼쪽으로 순회 | ||
# range(start, stop, step) => 반복을 시작할 값, 반복을 멈출 값, 반복의 증가 또는 감소 단위 | ||
for i in range(n - 1, -1, -1): | ||
result[i] *= right | ||
right *= nums[i] | ||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def is_anagram(s: str, t: str) -> bool: | ||
# return sorted(s) == sorted(t) | ||
''' | ||
Counter : 원소의 빈도수를 세는 자료구조, 한 번씩만 세면 됨 | ||
= O(n) | ||
Sorted : O(nlogn) | ||
''' | ||
return Counter(s) == Counter(t) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 트리를 중위 순회하기 왼쪽 -> 루트 -> 오른쪽 | ||
# 이전에 방문한 노드의 값이 현재 값보다 작은지 확인 | ||
# 모든 노드가 조건 만족하면 True | ||
|
||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
class Solution: | ||
def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
stack = [] | ||
# 최소값, 최대값 구할 때 사용 | ||
# float('-inf') : 음의 무한대 | ||
# float('inf') : 양의 무한대 | ||
prev = float('-inf') | ||
current = root | ||
|
||
# current가 존재하거나 stack이 비어있지 않은 한 계속 지속 | ||
while current or stack : | ||
while current: | ||
stack.append(current) | ||
current = current.left | ||
current = stack.pop() | ||
if current.val <= prev: | ||
return False | ||
prev = current.val | ||
current = current.right | ||
return True |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.