-
-
Notifications
You must be signed in to change notification settings - Fork 195
[gitsunmin] WEEK 01 Solutions #323
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
Changes from all commits
ace8fba
2ae9f86
e7c8954
029599b
ece065a
cd5e61f
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,8 @@ | ||
/** | ||
* https://leetcode.com/problems/contains-duplicate/ | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
function containsDuplicate(nums: number[]): boolean { | ||
return new Set(nums).size !== nums.length | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* https://leetcode.com/problems/kth-smallest-element-in-a-bst/ | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
|
||
/** | ||
* * 문제에서 정의된 타입 | ||
*/ | ||
class TreeNode { | ||
val: number | ||
left: TreeNode | null | ||
right: TreeNode | null | ||
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
this.val = (val === undefined ? 0 : val) | ||
this.left = (left === undefined ? null : left) | ||
this.right = (right === undefined ? null : right) | ||
} | ||
} | ||
|
||
/** | ||
* ! 문제에서의 Input과 실제 정의된, 사용되는 input이 다르기 때문에, 한 번 변환 작업을 거처야함. (실제 제출 시 제외한 함수 입니다.) | ||
*/ | ||
const arrayToTreeNode = (arr: Array<number | null>) => (k: number): [TreeNode | null, number] => { | ||
let root = new TreeNode(arr[0]!); | ||
let queue: (TreeNode | null)[] = [root]; | ||
let i = 1; | ||
|
||
while (i < arr.length) { | ||
let current = queue.shift(); | ||
if (current !== null && current !== undefined) { | ||
if (arr[i] !== null) { | ||
current.left = new TreeNode(arr[i]!); | ||
queue.push(current.left); | ||
} | ||
i++; | ||
if (i < arr.length && arr[i] !== null) { | ||
current.right = new TreeNode(arr[i]!); | ||
queue.push(current.right); | ||
} | ||
i++; | ||
} | ||
} | ||
|
||
return [root, k]; | ||
} | ||
|
||
// const input1 = arrayToTreeNode([3, 1, 4, null, 2]); | ||
// const input2 = arrayToTreeNode([5, 3, 6, 2, 4, null, null, 1]); | ||
|
||
// const output1 = kthSmallest(...input1(1)) | ||
// const output2 = kthSmallest(...input2(3)) | ||
|
||
// console.log('output1:', output1); | ||
// console.log('output2:', output2); | ||
|
||
|
||
function inOrderTraversal(node: TreeNode | null): number[] { | ||
if (node === null) { | ||
return []; | ||
} | ||
|
||
return [ | ||
...inOrderTraversal(node.left), | ||
node.val, | ||
...inOrderTraversal(node.right) | ||
]; | ||
} | ||
|
||
function kthSmallest(root: TreeNode | null, k: number): number { | ||
const result = inOrderTraversal(root); | ||
return result[k - 1] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* https://leetcode.com/problems/number-of-1-bits/ | ||
* time complexity : O(log n) | ||
* space complexity : O(log n) | ||
*/ | ||
function hammingWeight(n: number): number { | ||
return n.toString(2).replaceAll('0', '').length | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* https://leetcode.com/problems/palindromic-substrings/ | ||
* time complexity : O(n^2) | ||
* space complexity : O(n) | ||
*/ | ||
|
||
const expandAroundCenter = (s: string, left: number, right: number): number => { | ||
if (left < 0 || right >= s.length || s[left] !== s[right]) { | ||
return 0; | ||
} | ||
return 1 + expandAroundCenter(s, left - 1, right + 1); | ||
}; | ||
Comment on lines
+7
to
+12
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. 안녕하세요! expandAroundCenter 함수를 countSubstrings 외부로 뺀 이유가 있으실까요? 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. @nhistory 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. side effect를 고려하는 좋은 사고라고 생각합니다 어떤 분은 그런 pure function은 네이밍 컨벤션을 calc로 시작하도록 맞추기도 하시더라고요 |
||
|
||
function countSubstrings(s: string): number { | ||
|
||
return [...Array(s.length).keys()].reduce((totalCount, i) => { | ||
return totalCount + | ||
expandAroundCenter(s, i, i) + | ||
expandAroundCenter(s, i, i + 1); | ||
}, 0); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* https://leetcode.com/problems/top-k-frequent-elements/ | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const record = nums.reduce((acc, curr) => { | ||
acc[curr] = (acc[curr] ?? 0) + 1; | ||
return acc; | ||
}, {}); | ||
|
||
const array: Array<number[]> = new Array(nums.length); | ||
for (const num in record) { | ||
const v = record[num]; | ||
if (!array[v]) { | ||
array[v] = []; | ||
} | ||
array[v].push(Number(num)); | ||
} | ||
|
||
return array.reduce((acc, curr) => [...curr, ...acc], []).slice(0, k); | ||
}; |
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.
이렇게 간단하게 해결하는 방법도 있었네요! 👍👍