-
-
Notifications
You must be signed in to change notification settings - Fork 245
[선재] Week1 문제 풀이 #306
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
[선재] Week1 문제 풀이 #306
Changes from 4 commits
2706dea
cafa20c
e56eacb
af07ac3
30eb1b1
81fdc17
43c7341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @description | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
* approach/strategy: | ||
* 1. brute force + hash table | ||
*/ | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function (nums) { | ||
const hashTable = new Set(); | ||
|
||
for (const num of nums) { | ||
if (hashTable.has(num)) return true; | ||
hashTable.add(num); | ||
} | ||
|
||
return false; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @description | ||
* time complexity: O(N) | ||
* space complexity: O(N) | ||
* | ||
* brainstorming: | ||
* 1. BFS, DFS | ||
* 2. Brute force | ||
* | ||
* strategy: | ||
* inOrder search | ||
* | ||
* reason: | ||
* tree features | ||
*/ | ||
var kthSmallest = function (root, k) { | ||
let answer = 0; | ||
|
||
inOrder(root, (value) => { | ||
k -= 1; | ||
if (k > 0) return false; | ||
if (k === 0) answer = value; | ||
return true; | ||
}); | ||
|
||
return answer; | ||
}; | ||
|
||
function inOrder(tree, isEnd) { | ||
if (tree.left) inOrder(tree.left, isEnd); | ||
if (isEnd(tree.val)) return; | ||
if (tree.right) inOrder(tree.right, isEnd); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @description | ||
* time complexity: O(logN) | ||
* space complexity: O(1) | ||
* approach/strategy: | ||
* 1. decimal to binary | ||
*/ | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var hammingWeight = function (n) { | ||
let answer = 0; | ||
|
||
while (n > 0) { | ||
answer += n % 2; | ||
n = Math.floor(n / 2); | ||
} | ||
|
||
return answer; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @description | ||
* time complexity: O(N logN) | ||
* space complexity: O(N) | ||
* | ||
* brainstorming: | ||
* 1. javascript sort method | ||
* 2. priority queue | ||
* | ||
* strategy: | ||
* javascript sort method | ||
* | ||
* reason: | ||
* javascript sort method is easier to implement. | ||
*/ | ||
|
||
var topKFrequent = function (nums, k) { | ||
const answer = []; | ||
const array = []; | ||
const hashTable = new Map(); | ||
|
||
nums.forEach((num) => hashTable.set(num, (hashTable.get(num) ?? 0) + 1)); | ||
|
||
hashTable.forEach((count, number) => array.push({ number, count })); | ||
|
||
array.sort((a, b) => b.count - a.count); | ||
|
||
for (let i = 0; i < k; i++) answer.push(array[i].number); | ||
|
||
|
||
return answer; | ||
}; |
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.
이렇게 이유를 남겨주시니 완전 납득이 되네요 ㅋㅋㅋ
다음 주차까지 아직 시간이 많으니 brainstorming하셨던 priority queue를 사용해서도 풀어보시면 좋을 것 같습니다. 물론 현실 프로젝트에서는 간단하고 읽기 쉬운 코드가 더 선호되지만, 코딩 테스트에서는 결국 더 효율적인 알고리즘을 제시하는 것이 목표이니까요.