Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 18 additions & 0 deletions contains-duplicate/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function containsDuplicate(nums: number[]): boolean {
const dict: Set<number> = 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)
36 changes: 36 additions & 0 deletions kth-smallest-element-in-a-bst/whewchews.ts
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions number-of-1-bits/whewchews.ts
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions palindromic-substrings/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function countSubstrings(s: string): number {
// SC: O(N^2)
const dict: Map<string, boolean> = new Map();
const n = s.length;

// TC: O(N^2)
for (let start = 0; start < n; start++) {
for (let end = start; end >= 0; 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)
28 changes: 28 additions & 0 deletions top-k-frequent-elements/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function topKFrequent(nums: number[], k: number): number[] {
const dict: Map<number, number> = 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)