Skip to content

[lhc0506] WEEK 06 solutions #1423

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 5 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
32 changes: 32 additions & 0 deletions container-with-most-water/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let result = 0;
let left = 0;
let right = height.length - 1;

while(left < right) {
const leftHeight = height[left];
const rightHeight = height[right];
const width = right - left;

const minHeight = leftHeight < rightHeight ? leftHeight : rightHeight;
const area = minHeight * width;

if (area > result) {
result = area;
}

if (leftHeight <= rightHeight) {
left++;
} else {
right--;
}
}
return result;
};

// 시간 복잡도: O(n)
// 공간 복잡도: O(1)
67 changes: 67 additions & 0 deletions design-add-and-search-words-data-structure/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const Node = function() {
this.children = {};
this.isEnd = false;
}

var WordDictionary = function() {
this.root = new Node();
};

/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function(word) {
let currentNode = this.root;

for (let char of word) {
if (!currentNode.children[char]) {
currentNode.children[char] = new Node();
}
currentNode = currentNode.children[char];
}

currentNode.isEnd = true;
};

/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function(word) {
if (word === undefined) return false;
return this._search(this.root, word, 0);
};

WordDictionary.prototype._search = function(node, word, index) {
if (index === word.length) {
return node.isEnd;
}

const char = word[index];

if (char !== '.') {
const child = node.children[char];
return child ? this._search(child, word, index + 1) : false;
}

for (const key in node.children) {
if (this._search(node.children[key], word, index + 1)) {
return true;
}
}

return false;
};

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/

// n: 단어수, m: 단어 길이
// addWord: 시간 복잡도 O(m)
// search: 시간 복잡도 O(m)
// 공간 복잡도 O(n * m)
20 changes: 20 additions & 0 deletions longest-increasing-subsequence/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number[]} nums
* @return {number}
*/
var lengthOfLIS = function(nums) {
const dp = new Array(nums.length).fill(1);

for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i] && dp[j] >= dp[i]) {
dp[i] = dp[j] + 1;
}
}
}

return Math.max(...dp);
};

// 시간 복잡도 : O(n^2)
// 공간 복잡도 : O(n)
41 changes: 41 additions & 0 deletions spiral-matrix/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if (matrix.length === 0) return [];

const result = [];
const rows = matrix.length;
const cols = matrix[0].length;

let top = 0, bottom = rows - 1, left = 0, right = cols - 1;

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

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

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

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

return result;
};
39 changes: 39 additions & 0 deletions valid-parentheses/lhc0506.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @param {string} s
* @return {boolean}
*/

const BRACKETS = {
SMALL_LEFT : '(',
SMALL_RIGHT : ')',
MIDIDUM_LEFT : '{',
MIDIDUM_RIGHT : '}',
LARGE_LEFT : '[',
LARGE_RIGHT : ']',
};

const LEFT_BRAKCETS_SET = new Set([BRACKETS.SMALL_LEFT, BRACKETS.MIDIDUM_LEFT, BRACKETS.LARGE_LEFT]);

const BRACKET_MAPPER = {
[BRACKETS.SMALL_RIGHT]: BRACKETS.SMALL_LEFT,
[BRACKETS.MIDIDUM_RIGHT]: BRACKETS.MIDIDUM_LEFT,
[BRACKETS.LARGE_RIGHT]: BRACKETS.LARGE_LEFT,
};

var isValid = function(s) {
const stack = [];

for (const bracket of s) {
if (LEFT_BRAKCETS_SET.has(bracket)) {
stack.push(bracket);
} else {
const poppedBracekt = stack.pop();

if (poppedBracekt !== BRACKET_MAPPER[bracket]) {
return false;
}
}
}

return stack.length === 0;
};