Skip to content

[이병현] 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 10 commits into from
Aug 17, 2024
7 changes: 7 additions & 0 deletions contains-duplicate/tolluset.ts
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;
}
42 changes: 42 additions & 0 deletions kth-smallest-element-in-a-bst/tolluset.ts
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!;
}
10 changes: 10 additions & 0 deletions number-of-1-bits/tolluset.ts
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;
}
43 changes: 43 additions & 0 deletions palindromic-substrings/tolluset.ts
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;
}
26 changes: 26 additions & 0 deletions top-k-frequent-elements/tolluset.ts
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);
Comment on lines +23 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 함수 합성을 사용하는 방법도 있군요! 잘 봤습니다.