Skip to content

Commit a680861

Browse files
authored
Merge pull request #307 from HC-kang/main
[강희찬] Week1 문제 풀이
2 parents 9b98f97 + 535930e commit a680861

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed

contains-duplicate/HC-kang.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
217. Contains Duplicate
3+
4+
Example 1:
5+
Input: nums = [1,2,3,1]
6+
Output: true
7+
8+
Example 2:
9+
Input: nums = [1,2,3,4]
10+
Output: false
11+
12+
Example 3:
13+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
14+
Output: true
15+
*/
16+
17+
// Time complexity: O(n)
18+
// Space complexity: O(n)
19+
function containsDuplicate(nums: number[]): boolean {
20+
return nums.length !== new Set(nums).size;
21+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
230. Kth Smallest Element in a BST
3+
4+
Example 1:
5+
3
6+
/ \
7+
1 4
8+
\
9+
2
10+
Input: root = [3,1,4,null,2], k = 1
11+
Output: 1
12+
13+
Example 2:
14+
5
15+
/ \
16+
3 6
17+
/ \
18+
2 4
19+
/
20+
1
21+
Input: root = [5,3,6,2,4,null,null,1], k = 3
22+
Output: 3
23+
*/
24+
25+
// class TreeNode {
26+
// val: number
27+
// left: TreeNode | null
28+
// right: TreeNode | null
29+
// constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
30+
// this.val = (val===undefined ? 0 : val)
31+
// this.left = (left===undefined ? null : left)
32+
// this.right = (right===undefined ? null : right)
33+
// }
34+
// }
35+
36+
// Time complexity: O(n)
37+
// Space complexity: O(n)
38+
function kthSmallest(root: TreeNode | null, k: number): number {
39+
function inorder(root: TreeNode | null, arr: number[]) {
40+
if (!root) return;
41+
42+
inorder(root.left, arr);
43+
arr.push(root.val);
44+
inorder(root.right, arr);
45+
}
46+
47+
const arr: number[] = [];
48+
inorder(root, arr);
49+
return arr[k - 1];
50+
}
51+
52+
/**
53+
* NOT MY SOLUTION - NO NEED TO REVIEW
54+
* Awesome solution from leetcode
55+
*/
56+
function kthSmallest(root: TreeNode | null, k: number): number {
57+
/*
58+
define. return the kth smallest value in a BST.
59+
assess. smallest value can be 0. At least k nodes, which can be at its smallest, 1.
60+
approach. DFS with backtracking. Traverse down the left edges until we hit null. if k is 1, return that value. Else, backtrack, go right, then try to go left again. Use a stack.
61+
*/
62+
const stack = [];
63+
64+
let currentRank = 0;
65+
66+
let currentNode = root;
67+
68+
while (currentNode || stack.length > 0) {
69+
70+
while (currentNode) {
71+
stack.push(currentNode);
72+
73+
currentNode = currentNode.left;
74+
}
75+
76+
currentNode = stack.pop();
77+
78+
currentRank++;
79+
80+
if (currentRank === k) return currentNode.val;
81+
82+
currentNode = currentNode.right;
83+
}
84+
85+
};

number-of-1-bits/HC-kang.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
191. Number of 1 Bits
3+
4+
Example 1:
5+
Input: n = 11
6+
Output: 3
7+
Explanation:
8+
The input binary string 1011 has a total of three set bits.
9+
10+
Example 2:
11+
Input: n = 128
12+
Output: 1
13+
Explanation:
14+
The input binary string 10000000 has a total of one set bit.
15+
16+
Example 3:
17+
Input: n = 2147483645
18+
Output: 30
19+
Explanation:
20+
The input binary string 1111111111111111111111111111101 has a total of thirty set bits.
21+
*/
22+
23+
function hammingWeight(n: number): number {
24+
// Time complexity: O(logn)
25+
// Space complexity: O(logn)
26+
// it has a better readability and not so bad in space complexity
27+
return n.toString(2).split('1').length - 1;
28+
29+
// Time complexity: O(logn)
30+
// Space complexity: O(1)
31+
// it's better in space complexity, but sometimes the bitwise operation is not easy to understand
32+
let count = 0;
33+
while (n !== 0) {
34+
count += n & 1;
35+
n >>>= 1;
36+
}
37+
return count;
38+
}

palindromic-substrings/HC-kang.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
647. Palindromic Substrings
3+
4+
Example 1:
5+
Input: s = "abc"
6+
Output: 3
7+
Explanation: Three palindromic strings: "a", "b", "c".
8+
9+
Example 2:
10+
Input: s = "aaa"
11+
Output: 6
12+
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
13+
*/
14+
15+
// Time complexity: O(n^3)
16+
// Space complexity: O(n^3)
17+
function countSubstrings(s: string): number {
18+
function isPalindrome(s: string): boolean {
19+
if (s.length === 0) return false;
20+
if (s.length === 1) return true;
21+
let left = 0;
22+
let right = s.length - 1;
23+
while (left < right) {
24+
if (s[left] !== s[right]) return false;
25+
left++;
26+
right--;
27+
}
28+
return true;
29+
}
30+
31+
const candidates = new Map<string, number>();
32+
for (let i = 0; i < s.length; i++) {
33+
for (let j = i + 1; j <= s.length; j++) {
34+
// this will cost both t.c. and s.c. O(n^3)
35+
candidates.set(s.slice(i, j), (candidates.get(s.slice(i, j)) || 0) + 1);
36+
}
37+
}
38+
39+
let count = 0;
40+
for (const [key, value] of candidates) {
41+
if (isPalindrome(key)) count += value
42+
}
43+
44+
return count;
45+
};
46+
47+
// Time complexity: O(n^3)
48+
// Space complexity: O(1)
49+
function countSubstrings(s: string): number {
50+
function isPalindrome(s: string): boolean {
51+
if (s.length === 0) return false;
52+
if (s.length === 1) return true;
53+
let left = 0;
54+
let right = s.length - 1;
55+
while (left < right) {
56+
if (s[left] !== s[right]) return false;
57+
left++;
58+
right--;
59+
}
60+
return true;
61+
}
62+
63+
let count = 0;
64+
for (let i = 0; i < s.length; i++) {
65+
for (let j = i + 1; j <= s.length; j++) {
66+
// this will cause t.c. O(n^3). need to optimize this part
67+
if (isPalindrome(s.slice(i, j))) count++;
68+
}
69+
}
70+
71+
return count;
72+
}
73+
74+
// Time complexity: O(n^2)
75+
// Space complexity: O(1)
76+
function countSubstrings(s: string): number {
77+
function expandIsPalindrome(left: number, right: number): number {
78+
let count = 0;
79+
while (left >= 0 && right < s.length && s[left] === s[right]) {
80+
count++;
81+
left--;
82+
right++;
83+
}
84+
return count;
85+
}
86+
87+
let count = 0;
88+
for (let i = 0; i < s.length; i++) {
89+
count += expandIsPalindrome(i, i);
90+
count += expandIsPalindrome(i, i + 1);
91+
}
92+
return count;
93+
}

top-k-frequent-elements/HC-kang.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
347. Top K Frequent Elements
3+
4+
Example 1:
5+
Input: nums = [1,1,1,2,2,3], k = 2
6+
Output: [1,2]
7+
8+
Example 2:
9+
Input: nums = [1], k = 1
10+
Output: [1]
11+
*/
12+
13+
// Time complexity: O(nlogn)
14+
// Space complexity: O(n)
15+
function topKFrequent(nums: number[], k: number): number[] {
16+
const frequentMap = new Map<number, number>();
17+
for (const num of nums) { // s.c. O(n)
18+
frequentMap.set(num, (frequentMap.get(num) || 0) + 1);
19+
}
20+
return Array.from(frequentMap.entries())
21+
.sort((a, b) => b[1] - a[1]) // this will cost t.c. O(nlogn)
22+
.slice(0, k)
23+
.map((v) => v[0]);
24+
}

0 commit comments

Comments
 (0)