-
-
Notifications
You must be signed in to change notification settings - Fork 195
[hi-rachel] Week 02 Solutions #1216
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
7 commits
Select commit
Hold shift + click to select a range
aa17983
climbing stairs solution (ts)
hi-rachel 9a0fb68
valid anagram solution
hi-rachel b991376
product of array except self solution(ts)
hi-rachel 08f4da9
validate binary search tree solution (py)
hi-rachel ab95a95
3sum solution (py)
hi-rachel 3ccb0fd
fix line lint
hi-rachel b7d76ef
fix line lint
hi-rachel 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,16 @@ | ||
# O(n^2) time, O(n) space | ||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
triplets = set() | ||
|
||
for i in range(len(nums) - 2): | ||
seen = set() | ||
for j in range(i + 1, len(nums)): | ||
complement = -(nums[i] + nums[j]) | ||
if complement in seen: | ||
triplet = [nums[i], nums[j], complement] | ||
triplets.add(tuple(sorted(triplet))) | ||
seen.add(nums[j]) | ||
|
||
return list(triplets) |
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,15 @@ | ||
// n steps의 계단 오르기 | ||
// 한 번에 1 혹은 2 steps 오르기 가능 | ||
// 오를 수 있는 방법의 수 반환해라 | ||
// O(n) time, O(n) space | ||
|
||
function climbStairs(n: number): number { | ||
let ways: number[] = []; | ||
ways[0] = 1; | ||
ways[1] = 2; | ||
|
||
for (let i = 2; i < n; i++) { | ||
ways[i] = ways[i - 1] + ways[i - 2]; | ||
} | ||
return ways[n - 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,29 @@ | ||
/** | ||
* 요구사항 | ||
* answer이라는 새로운 배열을 만들어야 한다. | ||
* 이 배열에서 answer[i]는 | ||
* nums[i] 자기 자신을 제외한 나머지 모든 원소들의 곱이 되어야 한다. | ||
* | ||
* 풀이 | ||
* 자기 자신을 제외한 곱 = 자기 왼쪽까지의 곱 x 자기 오른쪽 까지의 곱 | ||
* => 왼쪽 누적 곱 x 오른쪽 누적 곱 = answer | ||
* O(n) time, O(1) space | ||
*/ | ||
|
||
function productExceptSelf(nums: number[]): number[] { | ||
const n = nums.length; | ||
const result: number[] = new Array(n).fill(n); | ||
|
||
let leftProduct = 1; | ||
for (let i = 0; i < n; i++) { | ||
result[i] = leftProduct; | ||
leftProduct *= nums[i]; | ||
} | ||
|
||
let rightProduct = 1; | ||
for (let i = n - 1; i >= 0; i--) { | ||
result[i] *= rightProduct; | ||
rightProduct *= 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,27 @@ | ||
/** | ||
* O(n) time | ||
* O(문자수(s + t)) space | ||
*/ | ||
|
||
function isAnagram(s: string, t: string): boolean { | ||
let sMap = new Map(); | ||
let tMap = new Map(); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
sMap.set(s[i], sMap.get(s[i]) + 1 || 1); | ||
} | ||
|
||
for (let i = 0; i < t.length; i++) { | ||
tMap.set(t[i], tMap.get(t[i]) + 1 || 1); | ||
} | ||
|
||
function areMapsEqual(map1, map2) { | ||
if (map1.size !== map2.size) return false; | ||
|
||
for (let [key, value] of map1) { | ||
if (map2.get(key) !== value) return false; | ||
} | ||
return true; | ||
} | ||
return areMapsEqual(sMap, tMap); | ||
} |
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,26 @@ | ||
# O(n) time, O(n) space | ||
|
||
# Definition for a binary tree node. | ||
# 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: | ||
def dfs(node, low, high): | ||
if not node: | ||
return True | ||
if not (low < node.val < high): | ||
return False | ||
return dfs(node.left, low, node.val) and dfs(node.right, node.val, high) | ||
|
||
return dfs(root, float('-inf'), float("inf")) |
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.
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.
이렇게 표현하는 방식은 처음 보았습니다. 😊