-
-
Notifications
You must be signed in to change notification settings - Fork 195
[솔방울] week 1 문제 풀이 #312
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
[솔방울] week 1 문제 풀이 #312
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0000e8
contains-duplicate solution
wooseok123 3ef3696
number of 1 bits solution
wooseok123 f8729d1
top k frequent elents solution
wooseok123 1bd2030
kth smallest element in a bst solution
wooseok123 7c47e34
palindromic substrings solution
wooseok123 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,7 @@ | ||
// TC : O(n) | SC : O(n) | ||
|
||
function containsDuplicate(nums) { | ||
let original_length = nums.length; | ||
let modified_length = new Set(nums).size; | ||
return original_length !== modified_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,19 @@ | ||
// TC : O(n log n) | SC : O(n) | ||
|
||
let findAllValuesInTree = (root, obj) => { | ||
obj[root.val] = true; | ||
if (!root.left && !root.right) return obj; | ||
if (root.left) findAllValuesInTree(root.left, obj); | ||
if (root.right) findAllValuesInTree(root.right, obj); | ||
|
||
return obj; | ||
}; | ||
|
||
var kthSmallest = function (root, k) { | ||
const obj = findAllValuesInTree(root, {}); | ||
const sortedList = Object.keys(obj) | ||
.map(Number) | ||
.sort((a, b) => a - b); | ||
|
||
return sortedList[k - 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,34 @@ | ||
let hammingWeight = function (n) { | ||
return DecToBinAndGetSetBits(n); | ||
}; | ||
|
||
// TC : O(log n) | SC : O(1) | ||
|
||
let DecToBinAndGetSetBits = (n) => { | ||
let targetNum = n; | ||
let result = 0; | ||
while (targetNum > 0) { | ||
let remainder = targetNum % 2; | ||
if (remainder === 1) result += 1; | ||
targetNum = parseInt(targetNum / 2); | ||
} | ||
return result; | ||
}; | ||
|
||
// TC : O(log n) | SC : O(log n) | ||
// 근데 사실 split 메서드 자체는 o(n)인데, toString과정을 통해 log(n)의 개수만큼 나와버린 것이면 o(log n)이라고 표기해도 되는걸까? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넹, 상관없을 것 같습니다. 내장 함수도 직접 구현하신 코드처럼 시간을 소모하니까요 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 애매한 부분이었는데 감사합니다 :) |
||
|
||
// let DecToBinAndGetSetBits = (n) => { | ||
// let target = n; | ||
// let bin = n.toString(2); | ||
// return bin.split("").filter((el) => el == 1).length | ||
// } | ||
|
||
// TC : O(log n) | SC : O(log n) | ||
|
||
// let DecToBinAndGetSetBits = (n) => { | ||
// let target = n; | ||
// let bin = n.toString(2); | ||
// let matches = bin.match(/1/g); | ||
// return matches.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,24 @@ | ||
var countSubstrings = function (s) { | ||
let result = 0; | ||
// 개수를 키워나가며, 각 자리가 대칭을 이루는지 검사한다. | ||
|
||
// substring의 개수 설정 | ||
for (let i = 0; i < s.length; i++) { | ||
// 시작점 설정 | ||
for (let j = 0; j < s.length - i; j++) { | ||
let isPalindromic = true; | ||
// 대칭되는 요소를 하나씩 비교 | ||
for (let k = j; k < Math.ceil((j * 2 + i) / 2); k++) { | ||
if (s[k] !== s[j * 2 + i - k]) { | ||
isPalindromic = false; | ||
break; | ||
} | ||
} | ||
if (isPalindromic) result += 1; | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// TC : o(n^3) | SC : o(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,15 @@ | ||
// TC : o(n log n) | SC : o(n) | ||
|
||
var topKFrequent = function (nums, k) { | ||
const elements = countElments(nums); | ||
const keys = Object.keys(elements).sort((a, b) => elements[b] - elements[a]); | ||
return keys.slice(0, k); | ||
}; | ||
|
||
let countElments = (nums) => { | ||
const temp = {}; | ||
for (const num of nums) { | ||
temp[num] = (count[num] || 0) + 1; | ||
} | ||
return temp; | ||
}; |
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.
안녕하세요, 사소한 리뷰 글 남깁니다~
풀이를 보았을 때 재귀적으로 트리를 순회하여 모든 노드의 값을 객체에 저장한 후,
정렬을 위해 한 번 더 모든 값을 순회하는 것으로 이해하였습니다.
시간 복잡도의 차이는 없겠지만, 순회하는 단계를 1회로 줄여보실 수도 있을 것 같네요!
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.
아하 BST에서 중위 순회를 하는 경우 이미 오름차순으로 정렬되기 때문에 추가로 순회하는 단계가 생략될 수 있다는 말씀이실까요!?