-
-
Notifications
You must be signed in to change notification settings - Fork 195
[선재] 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
Merged
Merged
[선재] Week1 문제 풀이 #306
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2706dea
Contains Duplicate
Sunjae95 cafa20c
Number of 1Bits
Sunjae95 e56eacb
Top K Frequent Elements
Sunjae95 af07ac3
Kth Smallest Element in a Bst
Sunjae95 30eb1b1
Palindromic Substrings
Sunjae95 81fdc17
top-k-frequent-elements 리뷰반영
Sunjae95 43c7341
Merge branch 'DaleStudy:main' into main
Sunjae95 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,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; | ||
}; |
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,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); | ||
} |
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,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; | ||
}; |
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,90 @@ | ||
/** | ||
* @description | ||
* time complexity: O(N^3) | ||
* space complexity: O(N) | ||
* | ||
* brainstorming: | ||
* 1. stack, permutation | ||
* 2. Brute force | ||
* | ||
* strategy: | ||
* Brute force, calculate | ||
* | ||
* reason: | ||
* intuitive way | ||
* | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var countSubstrings = function (s) { | ||
let answer = 0; | ||
const len = s.length; | ||
|
||
for (let i = 0; i < len; i++) { | ||
for (let j = i + 1; j < len + 1; j++) { | ||
const subStr = s.slice(i, j); | ||
if (isPalindrome(subStr)) answer++; | ||
} | ||
} | ||
|
||
return answer; | ||
}; | ||
|
||
function isPalindrome(str) { | ||
const len = str.length; | ||
const middleIndex = Math.floor(len / 2); | ||
|
||
for (let i = 0; i < middleIndex; i++) { | ||
if (str[i] !== str[len - 1 - i]) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @description | ||
* time complexity: O(N^2) | ||
* space complexity: O(N^2) | ||
* | ||
* brainstorming: | ||
* 1. https://sbslc.tistory.com/56 | ||
* | ||
* strategy: | ||
* dynamic programming | ||
* | ||
* reason: | ||
* to challenge dp | ||
* | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var countSubstrings = function (s) { | ||
const answer = []; | ||
const MAX_LENGTH = s.length; | ||
const dp = Array.from({ length: MAX_LENGTH }, (_, i) => | ||
Array.from({ length: MAX_LENGTH }, (_, j) => { | ||
if (i === j) answer.push(s[i]); | ||
return i === j; | ||
}) | ||
); | ||
// Check next step ex) aa, bb, cc | ||
for (let i = 0; i < MAX_LENGTH - 1; i++) { | ||
const nextIndex = i + 1; | ||
if (s[i] === s[nextIndex]) { | ||
dp[i][nextIndex] = true; | ||
answer.push(s.slice(i, nextIndex + 1)); | ||
} | ||
} | ||
// Check against values calculated in the previous step | ||
for (let len = 2; len <= MAX_LENGTH; len++) { | ||
for (let i = 0; i < MAX_LENGTH - len; i++) { | ||
const lastIndex = len + i; | ||
if (s[i] === s[lastIndex] && dp[i + 1][lastIndex - 1]) { | ||
dp[i][lastIndex] = true; | ||
answer.push(s.slice(i, lastIndex + 1)); | ||
} | ||
} | ||
} | ||
|
||
return answer.length; | ||
}; |
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,29 @@ | ||
/** | ||
* @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 hashTable = new Map(); | ||
|
||
nums.forEach((num) => hashTable.set(num, (hashTable.get(num) ?? 0) + 1)); | ||
|
||
hashTable.forEach((count, number) => answer.push({ number, count })); | ||
|
||
return answer | ||
.sort((a, b) => b.count - a.count) | ||
.slice(0, k) | ||
.map(({ number }) => number); | ||
}; |
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.
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를 사용해서도 풀어보시면 좋을 것 같습니다. 물론 현실 프로젝트에서는 간단하고 읽기 쉬운 코드가 더 선호되지만, 코딩 테스트에서는 결국 더 효율적인 알고리즘을 제시하는 것이 목표이니까요.