Skip to content

Commit ea9d6c0

Browse files
authored
Merge pull request DaleStudy#323 from gitsunmin/main
[gitsunmin] WEEK 01 Solutions
2 parents f4afc87 + cd5e61f commit ea9d6c0

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

contains-duplicate/gitsunmin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* https://leetcode.com/problems/contains-duplicate/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function containsDuplicate(nums: number[]): boolean {
7+
return new Set(nums).size !== nums.length
8+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* https://leetcode.com/problems/kth-smallest-element-in-a-bst/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
7+
/**
8+
* * 문제에서 정의된 타입
9+
*/
10+
class TreeNode {
11+
val: number
12+
left: TreeNode | null
13+
right: TreeNode | null
14+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
15+
this.val = (val === undefined ? 0 : val)
16+
this.left = (left === undefined ? null : left)
17+
this.right = (right === undefined ? null : right)
18+
}
19+
}
20+
21+
/**
22+
* ! 문제에서의 Input과 실제 정의된, 사용되는 input이 다르기 때문에, 한 번 변환 작업을 거처야함. (실제 제출 시 제외한 함수 입니다.)
23+
*/
24+
const arrayToTreeNode = (arr: Array<number | null>) => (k: number): [TreeNode | null, number] => {
25+
let root = new TreeNode(arr[0]!);
26+
let queue: (TreeNode | null)[] = [root];
27+
let i = 1;
28+
29+
while (i < arr.length) {
30+
let current = queue.shift();
31+
if (current !== null && current !== undefined) {
32+
if (arr[i] !== null) {
33+
current.left = new TreeNode(arr[i]!);
34+
queue.push(current.left);
35+
}
36+
i++;
37+
if (i < arr.length && arr[i] !== null) {
38+
current.right = new TreeNode(arr[i]!);
39+
queue.push(current.right);
40+
}
41+
i++;
42+
}
43+
}
44+
45+
return [root, k];
46+
}
47+
48+
// const input1 = arrayToTreeNode([3, 1, 4, null, 2]);
49+
// const input2 = arrayToTreeNode([5, 3, 6, 2, 4, null, null, 1]);
50+
51+
// const output1 = kthSmallest(...input1(1))
52+
// const output2 = kthSmallest(...input2(3))
53+
54+
// console.log('output1:', output1);
55+
// console.log('output2:', output2);
56+
57+
58+
function inOrderTraversal(node: TreeNode | null): number[] {
59+
if (node === null) {
60+
return [];
61+
}
62+
63+
return [
64+
...inOrderTraversal(node.left),
65+
node.val,
66+
...inOrderTraversal(node.right)
67+
];
68+
}
69+
70+
function kthSmallest(root: TreeNode | null, k: number): number {
71+
const result = inOrderTraversal(root);
72+
return result[k - 1]
73+
}

number-of-1-bits/gitsunmin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* https://leetcode.com/problems/number-of-1-bits/
3+
* time complexity : O(log n)
4+
* space complexity : O(log n)
5+
*/
6+
function hammingWeight(n: number): number {
7+
return n.toString(2).replaceAll('0', '').length
8+
}

palindromic-substrings/gitsunmin.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/palindromic-substrings/
3+
* time complexity : O(n^2)
4+
* space complexity : O(n)
5+
*/
6+
7+
const expandAroundCenter = (s: string, left: number, right: number): number => {
8+
if (left < 0 || right >= s.length || s[left] !== s[right]) {
9+
return 0;
10+
}
11+
return 1 + expandAroundCenter(s, left - 1, right + 1);
12+
};
13+
14+
function countSubstrings(s: string): number {
15+
16+
return [...Array(s.length).keys()].reduce((totalCount, i) => {
17+
return totalCount +
18+
expandAroundCenter(s, i, i) +
19+
expandAroundCenter(s, i, i + 1);
20+
}, 0);
21+
};

top-k-frequent-elements/gitsunmin.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/top-k-frequent-elements/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function topKFrequent(nums: number[], k: number): number[] {
7+
const record = nums.reduce((acc, curr) => {
8+
acc[curr] = (acc[curr] ?? 0) + 1;
9+
return acc;
10+
}, {});
11+
12+
const array: Array<number[]> = new Array(nums.length);
13+
for (const num in record) {
14+
const v = record[num];
15+
if (!array[v]) {
16+
array[v] = [];
17+
}
18+
array[v].push(Number(num));
19+
}
20+
21+
return array.reduce((acc, curr) => [...curr, ...acc], []).slice(0, k);
22+
};

0 commit comments

Comments
 (0)