Skip to content

Commit 43deec1

Browse files
committed
feat(week1): 문제 3개
1 parent 643d90f commit 43deec1

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

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+
};
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)