Skip to content

Commit c8870f6

Browse files
authored
Merge pull request #331 from whewchews/main
[pepper] Week 1 Solutions
2 parents 8c4fb50 + 5bd7b75 commit c8870f6

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

contains-duplicate/whewchews.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
const dict: Set<number> = new Set();
3+
4+
// O(n)
5+
for (let i = 0; i <= nums.length; i++) {
6+
const n = nums[i];
7+
8+
// O(1)
9+
if (dict.has(n)) return true;
10+
// O(1)
11+
dict.add(n);
12+
}
13+
14+
return false;
15+
}
16+
17+
// TC: O(N)
18+
// SC: O(N)
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function kthSmallest(root: TreeNode | null, k: number): number {
16+
// SC: O(N)
17+
const values: number[] = [];
18+
19+
// TC: O(N)
20+
const dfs = (node: TreeNode | null) => {
21+
if (node == null) return;
22+
dfs(node.left);
23+
values.push(node.val);
24+
dfs(node.right);
25+
};
26+
27+
// SC: O(h)
28+
// h: the height of the tree
29+
dfs(root);
30+
31+
// TC: O(1)
32+
return values[k - 1];
33+
}
34+
35+
// TC: O(N)
36+
// SC: O(N)

number-of-1-bits/whewchews.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function hammingWeight(n: number): number {
2+
// SC: log(1)
3+
let val = 0;
4+
while (n > 0) {
5+
val += n % 2;
6+
7+
// TC: log₂(n)
8+
n = Math.floor(n / 2);
9+
}
10+
11+
return val;
12+
}
13+
14+
// TC: O(log N)
15+
// SC: O(1)

palindromic-substrings/whewchews.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function countSubstrings(s: string): number {
2+
// SC: O(N^2)
3+
const dict: Map<string, boolean> = new Map();
4+
const n = s.length;
5+
6+
// TC: O(N^2)
7+
for (let start = n; start >= 0; start--) {
8+
for (let end = start; end < n; end++) {
9+
if (start === end) {
10+
dict.set(`${start}:${end}`, true);
11+
} else if (start + 1 === end) {
12+
dict.set(`${start}:${end}`, s[start] === s[end]);
13+
} else {
14+
const flag = s[start] === s[end];
15+
const mid = dict.get(`${start + 1}:${end - 1}`);
16+
dict.set(`${start}:${end}`, flag && mid);
17+
}
18+
}
19+
}
20+
21+
let cnt = 0;
22+
23+
// TC: O(N^2)
24+
// SC: O(1)
25+
for (const v of dict.values()) {
26+
if (v) {
27+
cnt++;
28+
}
29+
}
30+
31+
return cnt;
32+
}
33+
34+
// TC: O(N^2)
35+
// SC: O(N^2)

top-k-frequent-elements/whewchews.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function topKFrequent(nums: number[], k: number): number[] {
2+
const dict: Map<number, number> = new Map();
3+
4+
// TC: O(N)
5+
// SC: O(N)
6+
nums.forEach((n) => {
7+
if (!dict.has(n)) dict.set(n, 1);
8+
else {
9+
dict.set(n, dict.get(n) + 1);
10+
}
11+
});
12+
13+
// TC: O(N)
14+
// SC: O(N)
15+
const buckets: number[][] = Array(nums.length + 1)
16+
.fill(0)
17+
.map((_) => []);
18+
Array.from(dict.entries()).forEach(([num, cnt]) => {
19+
buckets[cnt].push(num);
20+
});
21+
22+
// TC: O(N) + O(k) = O(N)
23+
// SC: O(N)
24+
return buckets.flat().slice(-k);
25+
}
26+
27+
// TC: O(N)
28+
// SC: O(N)

0 commit comments

Comments
 (0)