Skip to content

Commit 446e0cc

Browse files
authored
Merge pull request #1433 from HoonDongKang/main
[HoonDongKang] WEEK 06 solutions
2 parents 1ea1795 + 1fa6b7d commit 446e0cc

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* [Problem]: [11] Container With Most Water
3+
* (https://leetcode.com/problems/container-with-most-water/description/)
4+
*/
5+
function maxArea(height: number[]): number {
6+
//시간복잡도 O(n)
7+
//공간복잡도 O(1)
8+
function twoPointerFunc(height: number[]): number {
9+
let left = 0;
10+
let right = height.length - 1;
11+
let result = 0;
12+
13+
while (left < right) {
14+
const length = Math.min(height[left], height[right]);
15+
const area = (right - left) * length;
16+
17+
result = Math.max(area, result);
18+
19+
if (height[left] < height[right]) {
20+
left++;
21+
} else {
22+
right--;
23+
}
24+
}
25+
26+
return result;
27+
}
28+
29+
return twoPointerFunc(height);
30+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* [Problem]: [211] Design Add and Search Words Data Structure
3+
* (https://leetcode.com/problems/design-add-and-search-words-data-structure/description/)
4+
*/
5+
class WordNode {
6+
children = new Map<string, WordNode>();
7+
isEnd: boolean = false;
8+
constructor() {}
9+
}
10+
11+
class WordDictionary {
12+
root = new WordNode();
13+
constructor() {}
14+
15+
//시간복잡도: O(n)
16+
//공간복잡도: O(n)
17+
addWord(word: string): void {
18+
let currentNode = this.root;
19+
20+
for (const char of word) {
21+
if (!currentNode.children.has(char)) {
22+
currentNode.children.set(char, new WordNode());
23+
}
24+
currentNode = currentNode.children.get(char)!;
25+
}
26+
27+
currentNode.isEnd = true;
28+
}
29+
30+
//시간복잡도: O(n)
31+
//공간복잡도: O(n)
32+
search(word: string): boolean {
33+
return this.dfsSearch(word, 0, this.root);
34+
}
35+
36+
private dfsSearch(word: string, index: number, node: WordNode): boolean {
37+
if (index === word.length) {
38+
return node.isEnd;
39+
}
40+
41+
const char = word[index];
42+
const isDot = char === ".";
43+
44+
if (isDot) {
45+
for (const child of node.children.values()) {
46+
if (this.dfsSearch(word, index + 1, child)) {
47+
return true;
48+
}
49+
}
50+
51+
return false;
52+
} else {
53+
const nextNode = node.children.get(char);
54+
if (!nextNode) return false;
55+
return this.dfsSearch(word, index + 1, nextNode);
56+
}
57+
}
58+
}
59+
60+
/**
61+
* Your WordDictionary object will be instantiated and called as such:
62+
* var obj = new WordDictionary()
63+
* obj.addWord(word)
64+
* var param_2 = obj.search(word)
65+
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* [Problem]: [300] Longest Increasing Subsequence
3+
* (https://leetcode.com/problems/longest-increasing-subsequence/)
4+
*/
5+
6+
function lengthOfLIS(nums: number[]): number {
7+
//시간복잡도 O(n^2)
8+
//공간복잡도 O(n)
9+
function dpFunc(nums: number[]): number {
10+
const dp = new Array(nums.length + 1).fill(1);
11+
12+
for (let i = 0; i < nums.length; i++) {
13+
for (let j = 0; j < i; j++) {
14+
if (nums[j] < nums[i]) {
15+
dp[i] = Math.max(dp[i], dp[j] + 1);
16+
}
17+
}
18+
}
19+
return Math.max(...dp);
20+
}
21+
22+
//시간복잡도 O(nlogn)
23+
//공간복잡도 O(n)
24+
function binearySearchFunc(nums: number[]): number {
25+
const results: number[] = [];
26+
27+
for (const num of nums) {
28+
let left = 0;
29+
let right = results.length;
30+
31+
while (left < right) {
32+
const mid = Math.floor((left + right) / 2);
33+
if (results[mid] < num) {
34+
left = mid + 1;
35+
} else {
36+
right = mid;
37+
}
38+
}
39+
40+
if (left < results.length) {
41+
results[left] = num;
42+
} else {
43+
results.push(num);
44+
}
45+
}
46+
47+
return results.length;
48+
}
49+
50+
return binearySearchFunc(nums);
51+
}

spiral-matrix/HoonDongKang.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* [Problem]: [54] Spiral Matrix
3+
* (https://leetcode.com/problems/spiral-matrix/description/)
4+
*/
5+
function spiralOrder(matrix: number[][]): number[] {
6+
const result: number[] = [];
7+
let top = 0;
8+
let bottom = matrix.length - 1;
9+
let left = 0;
10+
let right = matrix[0].length - 1;
11+
12+
while (top <= bottom && left <= right) {
13+
for (let i = left; i <= right; i++) {
14+
result.push(matrix[top][i]);
15+
}
16+
top++;
17+
18+
for (let i = top; i <= bottom; i++) {
19+
result.push(matrix[i][right]);
20+
}
21+
right--;
22+
23+
if (top <= bottom) {
24+
for (let i = right; i >= left; i--) {
25+
result.push(matrix[bottom][i]);
26+
}
27+
bottom--;
28+
}
29+
30+
if (left <= right) {
31+
for (let i = bottom; i >= top; i--) {
32+
result.push(matrix[i][left]);
33+
}
34+
left++;
35+
}
36+
}
37+
38+
return result;
39+
}

valid-parentheses/HoonDongKang.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* [Problem]: [20] Valid Parentheses
3+
* (https://leetcode.com/problems/valid-parentheses/)
4+
*/
5+
function isValid(s: string): boolean {
6+
//시간복잡도: O(n)
7+
//공간복잡도: O(n)
8+
const stack: string[] = [];
9+
const pairs: Record<string, string> = { ")": "(", "}": "{", "]": "[" };
10+
11+
for (const char of s) {
12+
if (["(", "{", "["].includes(char)) {
13+
stack.push(char);
14+
} else {
15+
if (stack.pop() !== pairs[char]) return false;
16+
}
17+
}
18+
19+
return stack.length === 0;
20+
}

0 commit comments

Comments
 (0)