Skip to content

Commit 1056830

Browse files
authored
Merge pull request #1216 from hi-rachel/main
[hi-rachel] Week 02 Solutions
2 parents 378debb + b7d76ef commit 1056830

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

โ€Ž3sum/hi-rachel.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# O(n^2) time, O(n) space
2+
3+
class Solution:
4+
def threeSum(self, nums: List[int]) -> List[List[int]]:
5+
triplets = set()
6+
7+
for i in range(len(nums) - 2):
8+
seen = set()
9+
for j in range(i + 1, len(nums)):
10+
complement = -(nums[i] + nums[j])
11+
if complement in seen:
12+
triplet = [nums[i], nums[j], complement]
13+
triplets.add(tuple(sorted(triplet)))
14+
seen.add(nums[j])
15+
16+
return list(triplets)

โ€Žclimbing-stairs/hi-rachel.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// n steps์˜ ๊ณ„๋‹จ ์˜ค๋ฅด๊ธฐ
2+
// ํ•œ ๋ฒˆ์— 1 ํ˜น์€ 2 steps ์˜ค๋ฅด๊ธฐ ๊ฐ€๋Šฅ
3+
// ์˜ค๋ฅผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜ ๋ฐ˜ํ™˜ํ•ด๋ผ
4+
// O(n) time, O(n) space
5+
6+
function climbStairs(n: number): number {
7+
let ways: number[] = [];
8+
ways[0] = 1;
9+
ways[1] = 2;
10+
11+
for (let i = 2; i < n; i++) {
12+
ways[i] = ways[i - 1] + ways[i - 2];
13+
}
14+
return ways[n - 1];
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ์š”๊ตฌ์‚ฌํ•ญ
3+
* answer์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.
4+
* ์ด ๋ฐฐ์—ด์—์„œ answer[i]๋Š”
5+
* nums[i] ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ชจ๋“  ์›์†Œ๋“ค์˜ ๊ณฑ์ด ๋˜์–ด์•ผ ํ•œ๋‹ค.
6+
*
7+
* ํ’€์ด
8+
* ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•œ ๊ณฑ = ์ž๊ธฐ ์™ผ์ชฝ๊นŒ์ง€์˜ ๊ณฑ x ์ž๊ธฐ ์˜ค๋ฅธ์ชฝ ๊นŒ์ง€์˜ ๊ณฑ
9+
* => ์™ผ์ชฝ ๋ˆ„์  ๊ณฑ x ์˜ค๋ฅธ์ชฝ ๋ˆ„์  ๊ณฑ = answer
10+
* O(n) time, O(1) space
11+
*/
12+
13+
function productExceptSelf(nums: number[]): number[] {
14+
const n = nums.length;
15+
const result: number[] = new Array(n).fill(n);
16+
17+
let leftProduct = 1;
18+
for (let i = 0; i < n; i++) {
19+
result[i] = leftProduct;
20+
leftProduct *= nums[i];
21+
}
22+
23+
let rightProduct = 1;
24+
for (let i = n - 1; i >= 0; i--) {
25+
result[i] *= rightProduct;
26+
rightProduct *= nums[i];
27+
}
28+
return result;
29+
}

โ€Žvalid-anagram/hi-rachel.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* O(n) time
3+
* O(๋ฌธ์ž์ˆ˜(s + t)) space
4+
*/
5+
6+
function isAnagram(s: string, t: string): boolean {
7+
let sMap = new Map();
8+
let tMap = new Map();
9+
10+
for (let i = 0; i < s.length; i++) {
11+
sMap.set(s[i], sMap.get(s[i]) + 1 || 1);
12+
}
13+
14+
for (let i = 0; i < t.length; i++) {
15+
tMap.set(t[i], tMap.get(t[i]) + 1 || 1);
16+
}
17+
18+
function areMapsEqual(map1, map2) {
19+
if (map1.size !== map2.size) return false;
20+
21+
for (let [key, value] of map1) {
22+
if (map2.get(key) !== value) return false;
23+
}
24+
return true;
25+
}
26+
return areMapsEqual(sMap, tMap);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# O(n) time, O(n) space
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, val=0, left=None, right=None):
6+
# self.val = val
7+
# self.left = left
8+
# self.right = right
9+
10+
# ์ขŒ์ธก ์„œ๋ธŒ ํŠธ๋ฆฌ๋กœ ๋‚ด๋ ค๊ฐˆ ๋–„:
11+
# - ํ•˜ํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ํ•˜ํ•œ๊ฐ’
12+
# - ์ƒํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๊ฐ’
13+
# ์šฐ์ธก ์„œ๋ธŒ ํŠธ๋ฆฌ๋กœ ๋‚ด๋ ค๊ฐˆ ๋•Œ:
14+
# - ํ•˜ํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๊ฐ’
15+
# - ์ƒํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ์ƒํ•œ๊ฐ’
16+
17+
class Solution:
18+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
19+
def dfs(node, low, high):
20+
if not node:
21+
return True
22+
if not (low < node.val < high):
23+
return False
24+
return dfs(node.left, low, node.val) and dfs(node.right, node.val, high)
25+
26+
return dfs(root, float('-inf'), float("inf"))

0 commit comments

Comments
ย (0)