diff --git a/contains-duplicate/tolluset.ts b/contains-duplicate/tolluset.ts new file mode 100644 index 000000000..c1b31092b --- /dev/null +++ b/contains-duplicate/tolluset.ts @@ -0,0 +1,7 @@ +/* + * TC: O(n) + * SC: O(n) + * */ +function containsDuplicate(nums: number[]): boolean { + return nums.length !== new Set(nums).size; +} diff --git a/kth-smallest-element-in-a-bst/tolluset.ts b/kth-smallest-element-in-a-bst/tolluset.ts new file mode 100644 index 000000000..2534ca3dc --- /dev/null +++ b/kth-smallest-element-in-a-bst/tolluset.ts @@ -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!; +} diff --git a/number-of-1-bits/tolluset.ts b/number-of-1-bits/tolluset.ts new file mode 100644 index 000000000..1536cff88 --- /dev/null +++ b/number-of-1-bits/tolluset.ts @@ -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; +} diff --git a/palindromic-substrings/tolluset.ts b/palindromic-substrings/tolluset.ts new file mode 100644 index 000000000..42432d24f --- /dev/null +++ b/palindromic-substrings/tolluset.ts @@ -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; +} diff --git a/top-k-frequent-elements/tolluset.ts b/top-k-frequent-elements/tolluset.ts new file mode 100644 index 000000000..7aa69e8f6 --- /dev/null +++ b/top-k-frequent-elements/tolluset.ts @@ -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(), + ); + + const toValues = (map: Map) => 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);