Skip to content

[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 7 commits into from
Apr 13, 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
16 changes: 16 additions & 0 deletions 3sum/hi-rachel.py
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)
15 changes: 15 additions & 0 deletions climbing-stairs/hi-rachel.ts
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];
}
29 changes: 29 additions & 0 deletions product-of-array-except-self/hi-rachel.ts
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;
}
27 changes: 27 additions & 0 deletions valid-anagram/hi-rachel.ts
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 표현하는 방식은 처음 보았습니다. 😊

}

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);
}
26 changes: 26 additions & 0 deletions validate-binary-search-tree/hi-rachel.py
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"))