Skip to content

Commit 360c039

Browse files
authored
Merge pull request #2392 from soobing/week1
[soobing] WEEK 01 solutions
2 parents 8faed68 + d9744f4 commit 360c039

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

contains-duplicate/soobing3.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
const checkMap = new Map();
3+
for(const n of nums) {
4+
if(checkMap.has(n)) {
5+
return true;
6+
} else {
7+
checkMap.set(n, 1);
8+
}
9+
}
10+
return false;
11+
};

house-robber/soobing3.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function rob(nums: number[]): number {
2+
const dp = new Array(nums.length);
3+
dp[0] = nums[0];
4+
dp[1] = Math.max(nums[0], nums[1]);
5+
6+
for(let i = 2; i < nums.length; i++) {
7+
dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);
8+
}
9+
return dp[nums.length - 1];
10+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function longestConsecutive(nums: number[]): number {
2+
const set = new Set<number>(nums);
3+
let maxLength = nums.length > 0 ? 1 : 0;
4+
for(const num of set) {
5+
if(!set.has(num - 1)) {
6+
let length = 1;
7+
let current = num;
8+
while(set.has(current + 1)) {
9+
length++;
10+
current++;
11+
}
12+
maxLength = Math.max(maxLength, length);
13+
}
14+
}
15+
return maxLength;
16+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
class MyMinPriorityQueue<T> {
2+
private heap: T[] = [];
3+
private compare: (a: T, b: T) => number;
4+
5+
constructor(compare: (a: T, b: T) => number) {
6+
this.compare = compare;
7+
}
8+
9+
public get size() {
10+
return this.heap.length;
11+
}
12+
13+
public get peek() {
14+
return this.heap[0];
15+
}
16+
17+
public toArray() {
18+
return this.heap;
19+
}
20+
21+
public push(value: T) {
22+
this.heap.push(value);
23+
this.bubbleUp();
24+
}
25+
26+
public pop() {
27+
if(this.heap.length === 0) {
28+
return undefined;
29+
}
30+
31+
const min = this.heap[0];
32+
const last = this.heap.pop();
33+
if(this.heap.length === 0) {
34+
return min;
35+
}
36+
37+
this.heap[0] = last ?? undefined as T;
38+
this.bubbleDown();
39+
40+
return min;
41+
}
42+
43+
private bubbleDown() {
44+
let index = 0;
45+
46+
while(true) {
47+
let smallestIndex = index;
48+
let leftIndex = 2 * index + 1;
49+
let rightIndex = 2 * index + 2;
50+
51+
if(leftIndex < this.size && this.compare(this.heap[leftIndex], this.heap[smallestIndex]) < 0) {
52+
smallestIndex = leftIndex;
53+
}
54+
55+
if(rightIndex < this.size && this.compare(this.heap[rightIndex], this.heap[smallestIndex]) < 0) {
56+
smallestIndex = rightIndex;
57+
}
58+
59+
if(index === smallestIndex) break;
60+
[this.heap[index], this.heap[smallestIndex]] = [this.heap[smallestIndex], this.heap[index]];
61+
index = smallestIndex;
62+
}
63+
}
64+
65+
private bubbleUp() {
66+
let index = this.size - 1;
67+
68+
while(index > 0) {
69+
const parentIndex = Math.floor((index - 1) / 2)
70+
if (this.compare(this.heap[parentIndex], this.heap[index]) > 0) {
71+
[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]]
72+
}
73+
74+
index = parentIndex;
75+
}
76+
}
77+
}
78+
79+
function topKFrequent(nums: number[], k: number): number[] {
80+
const map = new Map<number, number>();
81+
82+
for (const n of nums) {
83+
map.set(n, (map.get(n) ?? 0) + 1);
84+
}
85+
86+
const minHeap = new MyMinPriorityQueue((a: number, b: number) => {
87+
return (map.get(a) ?? 0) - (map.get(b) ?? 0)
88+
});
89+
90+
for(const [key, count] of map) {
91+
if(minHeap.size < k) {
92+
minHeap.push(key)
93+
} else if((map.get(minHeap.peek) ?? 0) < (map.get(key) ?? 0)) {
94+
minHeap.pop()
95+
minHeap.push(key)
96+
}
97+
}
98+
return minHeap.toArray()
99+
}

two-sum/soobing3.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function twoSum(nums: number[], target: number): number[] {
2+
const map = new Map<number, number[]>();
3+
for(let i = 0; i < nums.length; i++) {
4+
const current = map.get(nums[i]);
5+
if(current) {
6+
current.push(i);
7+
} else {
8+
map.set(nums[i], [i]);
9+
}
10+
}
11+
12+
nums.sort((a, b) => a - b);
13+
let left = 0;
14+
let right = nums.length - 1;
15+
16+
while(left < right) {
17+
if(nums[left] + nums[right] === target) {
18+
return [map.get(nums[left])?.pop() ?? 0, map.get(nums[right])?.pop() ?? 0];
19+
}
20+
21+
if(nums[left] + nums[right] > target) {
22+
right--;
23+
} else {
24+
left++;
25+
}
26+
}
27+
28+
return [];
29+
};

0 commit comments

Comments
 (0)