-
-
Notifications
You must be signed in to change notification settings - Fork 195
[이병현] Week 1 #319
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 #319
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4eeebf3
solve: contains duplicate
tolluset c4397be
solve: number of 1 bits
tolluset a87dd08
solve: top k frequent elements
tolluset a093b40
solve: kth smallest element in a bst
tolluset 9d52d3e
solve: palindromic substrings
tolluset 29ec5eb
refacotr: apply review
tolluset f19566a
refacotr: add more detail description
tolluset 2ad5c0e
refacotr: rename variables more clearly
tolluset 381f802
refacotr: rename variables more descriptive
tolluset 9607c5d
refactor: make return simple
tolluset 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: number[]): boolean { | ||
return nums.length !== new Set(nums).size; | ||
} |
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 @@ | ||
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; | ||
} | ||
} | ||
|
||
/* | ||
* TC: O(n) | ||
* SC: O(n) | ||
* */ | ||
function kthSmallest(root: TreeNode, k: number): number { | ||
let count = 0; | ||
let result: null | number = null; | ||
|
||
const inOrder = (node: TreeNode | null) => { | ||
if (!node || result !== null) { | ||
return false; | ||
} | ||
|
||
if (inOrder(node.left)) { | ||
return true; | ||
} | ||
|
||
count++; | ||
|
||
if (count === k) { | ||
result = node.val; | ||
return true; | ||
} | ||
|
||
inOrder(node.right); | ||
}; | ||
|
||
inOrder(root); | ||
|
||
return result!; | ||
} |
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,10 @@ | ||
/* | ||
* TC: O(logn) | ||
* SC: O(logn) | ||
* */ | ||
function hammingWeight(n: number): number { | ||
return n | ||
.toString(2) | ||
.split("") | ||
.filter((s) => s === "1").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,43 @@ | ||
/* | ||
* TC: O(n) | ||
* SC: O(n) | ||
* */ | ||
function countSubstrings(s: string): number { | ||
const transformedString = "#" + s.split("").join("#") + "#"; | ||
const transformedStringLength = transformedString.length; | ||
const palindromeLengths = new Array(transformedStringLength).fill(0); | ||
let currentCenter = 0, | ||
rightBoundary = 0, | ||
totalPalindromeCount = 0; | ||
|
||
for (let i = 0; i < transformedStringLength; i++) { | ||
// If i is within the rightmost center, copy the palindromes value from the mirror | ||
if (i < rightBoundary) { | ||
palindromeLengths[i] = Math.min( | ||
rightBoundary - i, | ||
palindromeLengths[currentCenter * 2 - i], | ||
); | ||
} | ||
|
||
// Expand around i until it's not a palindrome and not over left or right | ||
while ( | ||
i + palindromeLengths[i] + 1 < transformedStringLength && | ||
i - palindromeLengths[i] - 1 >= 0 && | ||
transformedString[i + palindromeLengths[i] + 1] === | ||
transformedString[i - palindromeLengths[i] - 1] | ||
) { | ||
palindromeLengths[i]++; | ||
} | ||
|
||
// If palindromes value is the new rightmost center, update center and right | ||
if (i + palindromeLengths[i] > radius) { | ||
currentCenter = i; | ||
rightBoundary = i + palindromeLengths[i]; | ||
} | ||
|
||
// Add the number of palindromes with center i to the result | ||
total += Math.floor((palindromeLengths[i] + 1) / 2); | ||
} | ||
|
||
return total; | ||
} |
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,26 @@ | ||
type Nums = [number, number][]; | ||
|
||
/* | ||
* TC: O(nlogn) | ||
* SC: O(n) | ||
* */ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const counting = (arr: number[]) => | ||
arr.reduce( | ||
(acc, n) => acc.set(n, (acc.get(n) ?? 0) + 1), | ||
new Map<number, number>(), | ||
); | ||
|
||
const toValues = (map: Map<number, number>) => Array.from(map.entries()); | ||
|
||
const sorting = (arr: Nums) => arr.sort((a, b) => b[1] - a[1]); | ||
|
||
const getK = (arr: Nums, k: number) => arr.slice(0, k).map((v) => v[0]); | ||
|
||
return pipe(counting, toValues, sorting, (arr: Nums) => getK(arr, k))(nums); | ||
} | ||
|
||
const pipe = | ||
(...fns: Function[]) => | ||
(x: any) => | ||
fns.reduce((v, f) => f(v), x); | ||
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.
이렇게 함수 합성을 사용하는 방법도 있군요! 잘 봤습니다.