Skip to content

Commit 291e04b

Browse files
authored
Merge pull request #2374 from sadie100/main
[sadie100] WEEK 01 solutions
2 parents 979169f + 52b9dfd commit 291e04b

5 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
풀이
3+
- JS μ§‘ν•© 자료ꡬ쑰인 Set을 ν™œμš©ν•©λ‹ˆλ‹€. Set은 쀑볡값이 λ“€μ–΄μ˜¬ 경우 μ œκ±°λ˜μ–΄ κ³ μœ ν•œ κ°’λ“€λ§Œ κ°–λŠ” νŠΉμ„±μ΄ μžˆμŠ΅λ‹ˆλ‹€.
4+
- numsλ₯Ό Set으둜 λ°”κΎΈκ³  두 λ°μ΄ν„°μ˜ length(Set의 경우 size μ ‘κ·Όμž μ‚¬μš©)λ₯Ό λΉ„κ΅ν•©λ‹ˆλ‹€.
5+
- λ‹€λ₯Ό 경우 : Setμ—μ„œ 쀑볡값이 제거된 κ²½μš°μ΄λ―€λ‘œ trueλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
6+
- 같을 경우 : 쀑볡값이 μ—†λ˜ κ²½μš°μ΄λ―€λ‘œ falseλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
7+
8+
Big O
9+
- Time Complexity: O(N) (N은 nums의 길이)
10+
new Set(nums)λ₯Ό λ§Œλ“œλŠ” κ³Όμ •μ—μ„œ λ°°μ—΄μ˜ λͺ¨λ“  μ›μ†Œλ₯Ό μˆœνšŒν•˜λ©° Set에 μ‚½μž…ν•©λ‹ˆλ‹€. (μ‚½μž… 평균 μ‹œκ°„ O(1))
11+
numSet.size 및 nums.lengthλŠ” 각각 O(1)μ΄λ―€λ‘œ 총 O(N)의 μ‹œκ°„λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.
12+
- Space Complexity: O(N)
13+
Set에 μ΅œλŒ€ n개의 μš”μ†Œκ°€ μ €μž₯λ˜λ―€λ‘œ O(N)의 κ³΅κ°„λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.
14+
*/
15+
16+
function containsDuplicate(nums: number[]): boolean {
17+
const numSet = new Set(nums)
18+
return numSet.size !== nums.length
19+
}

β€Žhouse-robber/sadie100.tsβ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
dp 배열을 λ§Œλ“€κ³  각 μ›μ†Œ μˆœμ„œμ—μ„œ μ΅œλŒ€μ˜ 값을 계산, λ¦¬ν„΄ν•œλ‹€
3+
4+
μ‹œκ°„λ³΅μž‘λ„
5+
O(N) : 단일 순회
6+
7+
κ³΅κ°„λ³΅μž‘λ„
8+
O(N) : dp λ°°μ—΄
9+
*/
10+
11+
function rob(nums: number[]): number {
12+
const dp = []
13+
14+
for (let i = 0; i < nums.length; i++) {
15+
if (i < 1) {
16+
dp[i] = nums[i]
17+
} else if (i < 2) {
18+
dp[i] = Math.max(dp[i - 1], nums[i])
19+
} else {
20+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1])
21+
}
22+
}
23+
24+
return dp.at(-1)
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
numsλ₯Ό Set으둜 λ§Œλ“€μ–΄μ„œ 쀑볡을 μ œκ±°ν•˜κ³  μˆœνšŒν•˜λ©΄μ„œ μ‹œμž‘μ (n-1이 Set에 μ—†λŠ” 수)을 μ°Ύμ•„ μ—°μ†λœ 수λ₯Ό μ°Ύμ•„κ°€λ©° lengthλ₯Ό κ³„μ‚°ν•œλ‹€.
3+
μ‹œμž‘μ μ΄ μ•„λ‹Œ num(set에 num-1이 μžˆλŠ” 경우)λŠ” μˆœνšŒν•˜μ§€ μ•ŠλŠ”λ‹€.
4+
5+
μ‹œκ°„λ³΅μž‘λ„
6+
O(N) : 이쀑 λ°˜λ³΅λ¬Έμ΄μ§€λ§Œ μ‹œμž‘μ μ—μ„œλ§Œ νƒμƒ‰ν•˜λ―€λ‘œ κΉŠμ€ 쀑볡 μˆœνšŒκ°€ μ—†μŒ
7+
*/
8+
9+
function longestConsecutive(nums: number[]): number {
10+
if (nums.length === 0) return 0
11+
const numSet = new Set(nums)
12+
let result = 1
13+
14+
for (let num of numSet) {
15+
if (numSet.has(num - 1)) {
16+
continue
17+
}
18+
19+
let next = num + 1
20+
let count = 1
21+
while (numSet.has(next)) {
22+
count += 1
23+
next += 1
24+
}
25+
26+
result = Math.max(result, count)
27+
}
28+
29+
return result
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
numsλ₯Ό μˆœνšŒν•˜λ©° {[num]: λ“±μž₯횟수} ν˜•νƒœμ˜ λ°μ΄ν„°λ‘œ λ°”κΎΈκ³ , μˆœνšŒκ°€ λλ‚œν›„
3+
ν•΄λ‹Ή 데이터λ₯Ό λ“±μž₯횟수 κΈ°μ€€μœΌλ‘œ λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ν•˜μ—¬ μƒμœ„ k개λ₯Ό λ¦¬ν„΄ν•œλ‹€
4+
5+
O(N)
6+
μ‹œκ°„λ³΅μž‘λ„ : O(NlogN)
7+
nums 순회 - O(N), 객체 λ°°μ—΄ν™” - O(N), μ •λ ¬ - O(NLogN) 도합 O(NLogN)
8+
*/
9+
10+
function topKFrequent(nums: number[], k: number): number[] {
11+
const countObj: Record<number, number> = {}
12+
13+
for (let num of nums) {
14+
if (num in countObj) {
15+
countObj[num] += 1
16+
} else {
17+
countObj[num] = 1
18+
}
19+
}
20+
21+
const countArr = Object.entries(countObj)
22+
.sort((a, b) => b[1] - a[1])
23+
.map(([num, cnt]) => Number(num))
24+
.filter((val, idx) => idx < k)
25+
26+
return countArr
27+
}

β€Žtwo-sum/sadie100.tsβ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
풀이
3+
ν•΄μ‹œλ§΅μ„ μ΄μš©ν•΄ ν˜„μž¬ κ°’μ˜ 보수(target - num)κ°€ 이미 λ“±μž₯ν–ˆλŠ”μ§€ ν™•μΈν•œλ‹€.
4+
5+
6+
μ‹œκ°„λ³΅μž‘λ„
7+
O(N) - nums 순회
8+
9+
κ³΅κ°„λ³΅μž‘λ„
10+
O(N) - numObj 생성
11+
*/
12+
13+
function twoSum(nums: number[], target: number): number[] {
14+
const numObj: { [key: number]: number } = {}
15+
16+
for (let i = 0; i < nums.length; i++) {
17+
const num = nums[i]
18+
if (target - num in numObj) {
19+
return [i, numObj[target - num]]
20+
} else {
21+
numObj[num] = i
22+
}
23+
}
24+
25+
return []
26+
}

0 commit comments

Comments
Β (0)