Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
27 changes: 27 additions & 0 deletions top-k-frequent-elements/tolluset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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) => {
const val = acc.get(n) ?? 0;
acc.set(n, val + 1);
return acc;
}, 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.

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