Skip to content

Commit bdc484b

Browse files
authored
Merge pull request #319 from tolluset/main
[이병현] Week 1
2 parents a680861 + 9607c5d commit bdc484b

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

contains-duplicate/tolluset.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function containsDuplicate(nums: number[]): boolean {
6+
return nums.length !== new Set(nums).size;
7+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/*
13+
* TC: O(n)
14+
* SC: O(n)
15+
* */
16+
function kthSmallest(root: TreeNode, k: number): number {
17+
let count = 0;
18+
let result: null | number = null;
19+
20+
const inOrder = (node: TreeNode | null) => {
21+
if (!node || result !== null) {
22+
return false;
23+
}
24+
25+
if (inOrder(node.left)) {
26+
return true;
27+
}
28+
29+
count++;
30+
31+
if (count === k) {
32+
result = node.val;
33+
return true;
34+
}
35+
36+
inOrder(node.right);
37+
};
38+
39+
inOrder(root);
40+
41+
return result!;
42+
}

number-of-1-bits/tolluset.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* TC: O(logn)
3+
* SC: O(logn)
4+
* */
5+
function hammingWeight(n: number): number {
6+
return n
7+
.toString(2)
8+
.split("")
9+
.filter((s) => s === "1").length;
10+
}

palindromic-substrings/tolluset.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function countSubstrings(s: string): number {
6+
const transformedString = "#" + s.split("").join("#") + "#";
7+
const transformedStringLength = transformedString.length;
8+
const palindromeLengths = new Array(transformedStringLength).fill(0);
9+
let currentCenter = 0,
10+
rightBoundary = 0,
11+
totalPalindromeCount = 0;
12+
13+
for (let i = 0; i < transformedStringLength; i++) {
14+
// If i is within the rightmost center, copy the palindromes value from the mirror
15+
if (i < rightBoundary) {
16+
palindromeLengths[i] = Math.min(
17+
rightBoundary - i,
18+
palindromeLengths[currentCenter * 2 - i],
19+
);
20+
}
21+
22+
// Expand around i until it's not a palindrome and not over left or right
23+
while (
24+
i + palindromeLengths[i] + 1 < transformedStringLength &&
25+
i - palindromeLengths[i] - 1 >= 0 &&
26+
transformedString[i + palindromeLengths[i] + 1] ===
27+
transformedString[i - palindromeLengths[i] - 1]
28+
) {
29+
palindromeLengths[i]++;
30+
}
31+
32+
// If palindromes value is the new rightmost center, update center and right
33+
if (i + palindromeLengths[i] > radius) {
34+
currentCenter = i;
35+
rightBoundary = i + palindromeLengths[i];
36+
}
37+
38+
// Add the number of palindromes with center i to the result
39+
total += Math.floor((palindromeLengths[i] + 1) / 2);
40+
}
41+
42+
return total;
43+
}

top-k-frequent-elements/tolluset.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type Nums = [number, number][];
2+
3+
/*
4+
* TC: O(nlogn)
5+
* SC: O(n)
6+
* */
7+
function topKFrequent(nums: number[], k: number): number[] {
8+
const counting = (arr: number[]) =>
9+
arr.reduce(
10+
(acc, n) => acc.set(n, (acc.get(n) ?? 0) + 1),
11+
new Map<number, number>(),
12+
);
13+
14+
const toValues = (map: Map<number, number>) => Array.from(map.entries());
15+
16+
const sorting = (arr: Nums) => arr.sort((a, b) => b[1] - a[1]);
17+
18+
const getK = (arr: Nums, k: number) => arr.slice(0, k).map((v) => v[0]);
19+
20+
return pipe(counting, toValues, sorting, (arr: Nums) => getK(arr, k))(nums);
21+
}
22+
23+
const pipe =
24+
(...fns: Function[]) =>
25+
(x: any) =>
26+
fns.reduce((v, f) => f(v), x);

0 commit comments

Comments
 (0)