-
-
Notifications
You must be signed in to change notification settings - Fork 245
[soobing] WEEK01 Solution #1675
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
347d259
feat(soobing): week1 > house-robber
soobing 632770a
feat(soobing): week1 > top-k-frequent-elements
soobing 22f71ca
feat(soobing): week1 > longest-consecutive-sequence
soobing 829fe66
feat(soobing): week1 > two-sum
soobing 643d90f
feat(soobing): week1 > contains-duplicate
soobing 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,42 @@ | ||
/** | ||
* 문제 유형: DP | ||
* | ||
* 문제 설명 | ||
* - 바로 옆집을 한번에 털 수 없을때, 최대로 털 수 있는 돈을 구하는 문제 | ||
* - 함정은, 홀수의 합 vs 짝수의 합만 비교해서는 안된다. 2개 초과해서 털 수 있는 경우가 있음 (ex. [2, 1, 1, 2]) | ||
* | ||
* 아이디어 | ||
* - DP 문제 답게 Top-down, Bottom-up 두 개 다 풀 수 있음 | ||
*/ | ||
|
||
function robBottomUp(nums: number[]): number { | ||
const n = nums.length; | ||
const dp = Array(n).fill(0); | ||
|
||
if (n === 1) return nums[0]; | ||
if (n === 2) return Math.max(nums[0], nums[1]); | ||
|
||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
for (let i = 2; i < n; i++) { | ||
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); | ||
} | ||
|
||
return dp[n - 1]; | ||
} | ||
|
||
function robTopDown(nums: number[]): number { | ||
const n = nums.length; | ||
const memo = new Map<number, number>(); | ||
|
||
const dp = (i: number) => { | ||
if (i < 0) return 0; | ||
if (memo.has(i)) return memo.get(i); | ||
|
||
const res = Math.max(dp(i - 1)!, dp(i - 2)! + nums[i]); | ||
memo.set(i, res); | ||
return res; | ||
}; | ||
return dp(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,93 @@ | ||
/** | ||
* 문제 유형: Heap | ||
* | ||
* 문제 설명 | ||
* - 가장 빈도수가 많은 K개 Element 추출 | ||
* | ||
* 아이디어 | ||
* 1) 빈도수 계산하여 index와 함께 Map에 저장, 빈도수를 기준으로 MinHeap 구성 | ||
* 2) 빈도수 Map을 순회하면서 MinHeap에 추가 + MinHeap의 크기가 K를 초과하면 최소값 제거 (미리미리 빈도수가 많은 K개 Element를 만들어나가는 형식) | ||
* 3) MinHeap에 남아있는 Element들의 index를 반환 | ||
* | ||
* 시간 복잡도: O(NlogK) | ||
* 공간 복잡도: O(N) | ||
*/ | ||
class MinHeap { | ||
heap: [number, number][] = []; | ||
hu6r1s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
insert(item: [number, number]) { | ||
this.heap.push(item); | ||
this.bubbleUp(); | ||
} | ||
|
||
private bubbleUp() { | ||
let index = this.heap.length - 1; | ||
while (index > 0) { | ||
const parentIndex = Math.floor((index - 1) / 2); | ||
if (this.heap[index][0] >= this.heap[parentIndex][0]) break; | ||
[this.heap[index], this.heap[parentIndex]] = [ | ||
this.heap[parentIndex], | ||
this.heap[index], | ||
]; | ||
index = parentIndex; | ||
} | ||
} | ||
|
||
extractMin(): [number, number] | undefined { | ||
if (this.heap.length === 0) return undefined; | ||
const min = this.heap[0]; | ||
const end = this.heap.pop(); | ||
if (this.heap.length > 0 && end !== undefined) { | ||
hu6r1s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.heap[0] = end; | ||
this.sinkDown(0); | ||
} | ||
return min; | ||
} | ||
|
||
private sinkDown(index: number) { | ||
const length = this.heap.length; | ||
while (true) { | ||
let left = 2 * index + 1; | ||
let right = 2 * index + 2; | ||
let smallest = index; | ||
|
||
if (left < length && this.heap[left][0] < this.heap[smallest][0]) { | ||
smallest = left; | ||
} | ||
if (right < length && this.heap[right][0] < this.heap[smallest][0]) { | ||
smallest = right; | ||
} | ||
if (index === smallest) break; | ||
|
||
[this.heap[index], this.heap[smallest]] = [ | ||
this.heap[smallest], | ||
this.heap[index], | ||
]; | ||
index = smallest; | ||
} | ||
} | ||
|
||
peek(): [number, number] | undefined { | ||
return this.heap[0]; | ||
} | ||
|
||
size(): number { | ||
return this.heap.length; | ||
} | ||
} | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const freqMap = new Map<number, number>(); | ||
for (const num of nums) { | ||
freqMap.set(num, (freqMap.get(num) ?? 0) + 1); | ||
} | ||
|
||
const minHeap = new MinHeap(); | ||
for (const [num, count] of freqMap.entries()) { | ||
minHeap.insert([count, num]); | ||
if (minHeap.size() > k) { | ||
minHeap.extractMin(); | ||
} | ||
} | ||
|
||
return minHeap.heap.map(([_, num]) => num); | ||
} |
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.