diff --git a/contains-duplicate/whewchews.ts b/contains-duplicate/whewchews.ts new file mode 100644 index 000000000..7a939eac4 --- /dev/null +++ b/contains-duplicate/whewchews.ts @@ -0,0 +1,18 @@ +function containsDuplicate(nums: number[]): boolean { + const dict: Set = new Set(); + + // O(n) + for (let i = 0; i <= nums.length; i++) { + const n = nums[i]; + + // O(1) + if (dict.has(n)) return true; + // O(1) + dict.add(n); + } + + return false; +} + +// TC: O(N) +// SC: O(N) diff --git a/kth-smallest-element-in-a-bst/whewchews.ts b/kth-smallest-element-in-a-bst/whewchews.ts new file mode 100644 index 000000000..65b632ec8 --- /dev/null +++ b/kth-smallest-element-in-a-bst/whewchews.ts @@ -0,0 +1,36 @@ +/** + * Definition for a binary tree node. + * 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) + * } + * } + */ + +function kthSmallest(root: TreeNode | null, k: number): number { + // SC: O(N) + const values: number[] = []; + + // TC: O(N) + const dfs = (node: TreeNode | null) => { + if (node == null) return; + dfs(node.left); + values.push(node.val); + dfs(node.right); + }; + + // SC: O(h) + // h: the height of the tree + dfs(root); + + // TC: O(1) + return values[k - 1]; +} + +// TC: O(N) +// SC: O(N) diff --git a/number-of-1-bits/whewchews.ts b/number-of-1-bits/whewchews.ts new file mode 100644 index 000000000..2e7ccf03c --- /dev/null +++ b/number-of-1-bits/whewchews.ts @@ -0,0 +1,15 @@ +function hammingWeight(n: number): number { + // SC: log(1) + let val = 0; + while (n > 0) { + val += n % 2; + + // TC: logâ‚‚(n) + n = Math.floor(n / 2); + } + + return val; +} + +// TC: O(log N) +// SC: O(1) diff --git a/palindromic-substrings/whewchews.ts b/palindromic-substrings/whewchews.ts new file mode 100644 index 000000000..fa9e302f1 --- /dev/null +++ b/palindromic-substrings/whewchews.ts @@ -0,0 +1,35 @@ +function countSubstrings(s: string): number { + // SC: O(N^2) + const dict: Map = new Map(); + const n = s.length; + + // TC: O(N^2) + for (let start = n; start >= 0; start--) { + for (let end = start; end < n; end++) { + if (start === end) { + dict.set(`${start}:${end}`, true); + } else if (start + 1 === end) { + dict.set(`${start}:${end}`, s[start] === s[end]); + } else { + const flag = s[start] === s[end]; + const mid = dict.get(`${start + 1}:${end - 1}`); + dict.set(`${start}:${end}`, flag && mid); + } + } + } + + let cnt = 0; + + // TC: O(N^2) + // SC: O(1) + for (const v of dict.values()) { + if (v) { + cnt++; + } + } + + return cnt; +} + +// TC: O(N^2) +// SC: O(N^2) diff --git a/top-k-frequent-elements/whewchews.ts b/top-k-frequent-elements/whewchews.ts new file mode 100644 index 000000000..633a6498a --- /dev/null +++ b/top-k-frequent-elements/whewchews.ts @@ -0,0 +1,28 @@ +function topKFrequent(nums: number[], k: number): number[] { + const dict: Map = new Map(); + + // TC: O(N) + // SC: O(N) + nums.forEach((n) => { + if (!dict.has(n)) dict.set(n, 1); + else { + dict.set(n, dict.get(n) + 1); + } + }); + + // TC: O(N) + // SC: O(N) + const buckets: number[][] = Array(nums.length + 1) + .fill(0) + .map((_) => []); + Array.from(dict.entries()).forEach(([num, cnt]) => { + buckets[cnt].push(num); + }); + + // TC: O(N) + O(k) = O(N) + // SC: O(N) + return buckets.flat().slice(-k); +} + +// TC: O(N) +// SC: O(N)