Skip to content

[HoonDongKang] WEEK 06 solutions #1433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions container-with-most-water/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* [Problem]: [11] Container With Most Water
* (https://leetcode.com/problems/container-with-most-water/description/)
*/
function maxArea(height: number[]): number {
//시간복잡도 O(n)
//공간복잡도 O(1)
function twoPointerFunc(height: number[]): number {
let left = 0;
let right = height.length - 1;
let result = 0;

while (left < right) {
const length = Math.min(height[left], height[right]);
const area = (right - left) * length;

result = Math.max(area, result);

if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return result;
}

return twoPointerFunc(height);
}
65 changes: 65 additions & 0 deletions design-add-and-search-words-data-structure/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* [Problem]: [211] Design Add and Search Words Data Structure
* (https://leetcode.com/problems/design-add-and-search-words-data-structure/description/)
*/
class WordNode {
children = new Map<string, WordNode>();
isEnd: boolean = false;
constructor() {}
}

class WordDictionary {
root = new WordNode();
constructor() {}

//시간복잡도: O(n)
//공간복잡도: O(n)
addWord(word: string): void {
let currentNode = this.root;

for (const char of word) {
if (!currentNode.children.has(char)) {
currentNode.children.set(char, new WordNode());
}
currentNode = currentNode.children.get(char)!;
}

currentNode.isEnd = true;
}

//시간복잡도: O(n)
//공간복잡도: O(n)
search(word: string): boolean {
return this.dfsSearch(word, 0, this.root);
}

private dfsSearch(word: string, index: number, node: WordNode): boolean {
if (index === word.length) {
return node.isEnd;
}

const char = word[index];
const isDot = char === ".";

if (isDot) {
for (const child of node.children.values()) {
if (this.dfsSearch(word, index + 1, child)) {
return true;
}
}

return false;
} else {
const nextNode = node.children.get(char);
if (!nextNode) return false;
return this.dfsSearch(word, index + 1, nextNode);
}
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/
51 changes: 51 additions & 0 deletions longest-increasing-subsequence/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* [Problem]: [300] Longest Increasing Subsequence
* (https://leetcode.com/problems/longest-increasing-subsequence/)
*/

function lengthOfLIS(nums: number[]): number {
//시간복잡도 O(n^2)
//공간복잡도 O(n)
function dpFunc(nums: number[]): number {
const dp = new Array(nums.length + 1).fill(1);

for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return Math.max(...dp);
}

//시간복잡도 O(nlogn)
//공간복잡도 O(n)
function binearySearchFunc(nums: number[]): number {
const results: number[] = [];

for (const num of nums) {
let left = 0;
let right = results.length;

while (left < right) {
const mid = Math.floor((left + right) / 2);
if (results[mid] < num) {
left = mid + 1;
} else {
right = mid;
}
}

if (left < results.length) {
results[left] = num;
} else {
results.push(num);
}
}

return results.length;
}

return binearySearchFunc(nums);
}
39 changes: 39 additions & 0 deletions spiral-matrix/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* [Problem]: [54] Spiral Matrix
* (https://leetcode.com/problems/spiral-matrix/description/)
*/
function spiralOrder(matrix: number[][]): number[] {
const result: number[] = [];
let top = 0;
let bottom = matrix.length - 1;
let left = 0;
let right = matrix[0].length - 1;

while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(matrix[top][i]);
}
top++;

for (let i = top; i <= bottom; i++) {
result.push(matrix[i][right]);
}
right--;

if (top <= bottom) {
for (let i = right; i >= left; i--) {
result.push(matrix[bottom][i]);
}
bottom--;
}

if (left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(matrix[i][left]);
}
left++;
}
}

return result;
}
20 changes: 20 additions & 0 deletions valid-parentheses/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* [Problem]: [20] Valid Parentheses
* (https://leetcode.com/problems/valid-parentheses/)
*/
function isValid(s: string): boolean {
//시간복잡도: O(n)
//공간복잡도: O(n)
const stack: string[] = [];
const pairs: Record<string, string> = { ")": "(", "}": "{", "]": "[" };

for (const char of s) {
if (["(", "{", "["].includes(char)) {
stack.push(char);
} else {
if (stack.pop() !== pairs[char]) return false;
}
}

return stack.length === 0;
}