Skip to content

Commit

Permalink
Merge pull request #462 from wogha95/main
Browse files Browse the repository at this point in the history
[์žฌํ˜ธ] WEEK 06 Solutions
  • Loading branch information
wogha95 authored Sep 21, 2024
2 parents cfc0c46 + e8f521c commit ce9ec5d
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 0 deletions.
33 changes: 33 additions & 0 deletions container-with-most-water/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* TC: O(H)
* SC: O(1)
* H: height.length
*/

/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let maximumWater = 0;
let left = 0;
let right = height.length - 1;

// 1. ํˆฌํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์–‘๋์—์„œ ๋ชจ์ž…๋‹ˆ๋‹ค.
while (left < right) {
// 2. ์ตœ๋Œ€ ๋„ˆ๋น„๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ฃผ๊ณ 
const h = Math.min(height[left], height[right]);
const w = right - left;

maximumWater = Math.max(maximumWater, w * h);

// 3. ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ๋†’์ด ์ค‘ ๋” ๋‚ฎ์€ ์ชฝ์˜ pointer๋ฅผ ์˜ฎ๊น๋‹ˆ๋‹ค.
if (height[left] < height[right]) {
left += 1;
} else {
right -= 1;
}
}

return maximumWater;
};
83 changes: 83 additions & 0 deletions design-add-and-search-words-data-structure/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
function Node() {
// ๋‹จ์–ด์˜ ๋์„ ์˜๋ฏธ + ๋ฌด์Šจ ๋‹จ์–ด์ธ์ง€ ์ €์žฅ
this.value = null;
// ๋‹จ์–ด์˜ ๋‹ค์Œ ๋ฌธ์ž๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋งต
this.wordGraph = new Map();
}

var WordDictionary = function () {
this.wordGraph = new Map();
};

/**
* TC: O(N)
* SC: O(N)
*/

/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function (word) {
let pointer = this;
for (const w of word) {
if (!pointer.wordGraph.has(w)) {
pointer.wordGraph.set(w, new Node());
}
pointer = pointer.wordGraph.get(w);
}
pointer.value = word;
};

/**
* TC: O(D^W)
* SC: O(D * W)
*
* W: word.length, D: count of Dictionary.wordGraph keys
*
* ํ’€์ด: Trie ์ž๋ฃŒ๊ตฌ์กฐ + bfsํƒ์ƒ‰
*/

/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function (word) {
const queue = [{ pointer: this, index: 0 }];

// 1. BFS ํƒ์ƒ‰ ๋ฐฉ๋ฒ• ์ด์šฉ
while (queue.length > 0) {
const { pointer, index } = queue.shift();

// 2. ์ฐพ๊ณ ์žํ•˜๋Š” ๋‹จ์–ด์˜ ๋์— ๋„๋‹ฌํ–ˆ์œผ๋ฉด ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
if (index === word.length) {
if (pointer.value !== null) {
return true;
}
continue;
}

if (word[index] === ".") {
// 3. ์ฐพ๊ณ ์žํ•˜๋Š” ๋‹จ์–ด์˜ ๋ฌธ์ž๊ฐ€ '.'์ธ ๊ฒฝ์šฐ, ํ˜„์žฌ graph์—์„œ ์ด์–ด์ง„ ๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰(queue์— ์ถ”๊ฐ€)
for (const [key, node] of pointer.wordGraph) {
queue.push({ pointer: node, index: index + 1 });
}
} else if (pointer.wordGraph.has(word[index])) {
// 4. ์ฐพ๊ณ ์žํ•˜๋Š” ๋‹จ์–ด์˜ ๋ฌธ์ž๊ฐ€ graph์— ์žˆ๋Š” ๊ฒฝ์šฐ ํƒ์ƒ‰(queue์— ์ถ”๊ฐ€)
queue.push({
pointer: pointer.wordGraph.get(word[index]),
index: index + 1,
});
}
}

// 5. ๋”์ด์ƒ ํƒ์ƒ‰ํ•  ๊ฒƒ์ด ์—†๋‹ค๋ฉด ํ•ด๋‹น ๋‹จ์–ด ์—†์Œ์œผ๋กœ ํŒ๋‹จ
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)
*/
30 changes: 30 additions & 0 deletions longest-increasing-subsequence/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* TC: O(N^2)
* SC: O(N)
* N: nums.length
*/

/**
* @param {number[]} nums
* @return {number}
*/
var lengthOfLIS = function (nums) {
// ๊ฐ์ž ์Šค์Šค๋กœ๋Š” ์ตœ์†Œ 1์˜ lengthOfLIS๋ฅผ ๊ฐ€์ง
const longestLength = new Array(nums.length).fill(1);
let result = 1;

// nums๋ฐฐ์—ด์˜ right๊นŒ์ง€ ์›์†Œ๋“ค ์ค‘ lengthOfLIS๋ฅผ ์ €์žฅ
for (let right = 1; right < nums.length; right++) {
for (let left = 0; left < right; left++) {
if (nums[left] < nums[right]) {
longestLength[right] = Math.max(
longestLength[right],
longestLength[left] + 1
);
result = Math.max(result, longestLength[right]);
}
}
}

return result;
};
140 changes: 140 additions & 0 deletions spiral-matrix/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* 2์ฐจ ํ’€์ด: ๊ธฐ์กด matrix ๋ณ€๊ฒฝ ์—†๋„๋ก ๊ฐœ์„ 
*
* TC: O(ROW * COLUMN)
* matrix ์ „์ฒด ์ˆœํšŒ 1ํšŒ
*
* SC: O(ROW * COLUMN)
* ์ •๋‹ต ์ œ์ถœ์„ ์œ„ํ•œ result ๊ณต๊ฐ„๋ณต์žก๋„
*/

/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
const ROW = matrix.length;
const COLUMN = matrix[0].length;
// 1. ์ƒํ•˜์ขŒ์šฐ ์‹œ์ž‘๋ index๋ฅผ ์ €์žฅํ•จ
const boundary = {
top: 0,
bottom: ROW - 1,
left: 0,
right: COLUMN - 1,
};
const result = [];

while (result.length < ROW * COLUMN) {
// 2. ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ˆœํšŒ
for (let column = boundary.left; column <= boundary.right; column++) {
result.push(matrix[boundary.top][column]);
}
boundary.top += 1;

// 3. ์•„๋ž˜๋กœ ์ˆœํšŒ
for (let row = boundary.top; row <= boundary.bottom; row++) {
result.push(matrix[row][boundary.right]);
}
boundary.right -= 1;

// 4. ๋ชจ๋‘ ์ˆœํšŒํ–ˆ๋Š”๋ฐ ์™”๋˜๊ธธ ๋˜๋Œ์•„๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ง‰๊ธฐ์œ„ํ•ด ์ค‘๊ฐ„ ์กฐ๊ฑด๋ฌธ ์ถ”๊ฐ€
if (result.length === ROW * COLUMN) {
break;
}

// 5. ์™ผ์ชฝ์œผ๋กœ ์ˆœํšŒ
for (let column = boundary.right; column >= boundary.left; column--) {
result.push(matrix[boundary.bottom][column]);
}
boundary.bottom -= 1;

// 6. ์œ„์ชฝ์œผ๋กœ ์ˆœํšŒ
for (let row = boundary.bottom; row >= boundary.top; row--) {
result.push(matrix[row][boundary.left]);
}
boundary.left += 1;
}

return result;
};

/**
* 1์ฐจ ํ’€์ด
*
* TC: O(ROW * COLUMN)
* matrix ์ „์ฒด ์ˆœํšŒ 1ํšŒ
*
* SC: O(ROW * COLUMN)
* ์ •๋‹ต ์ œ์ถœ์„ ์œ„ํ•œ result ๊ณต๊ฐ„๋ณต์žก๋„
*/

/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function (matrix) {
const ROW = matrix.length;
const COLUMN = matrix[0].length;
// ์šฐํ•˜์ขŒ์ƒ ์ˆœ์„œ
const DIRECTION = [
{
row: 0,
column: 1,
},
{
row: 1,
column: 0,
},
{
row: 0,
column: -1,
},
{
row: -1,
column: 0,
},
];

// 1. ์ฒซ ์‹œ์ž‘์  ๋ฐฉ๋ฌธํ‘œ์‹œ
const result = [matrix[0][0]];
matrix[0][0] = "#";

let current = {
row: 0,
column: 0,
};
let directionIndex = 0;

// 2. ์ด ๊ฐฏ์ˆ˜๋งŒํผ ์ฑ„์›Œ์งˆ๋•Œ๊นŒ์ง€ ์ˆœํšŒ
while (result.length < ROW * COLUMN) {
const next = {
row: current.row + DIRECTION[directionIndex].row,
column: current.column + DIRECTION[directionIndex].column,
};
// 3. ๋‹ค์Œ ์ˆœํšŒํ•  ๊ณณ์ด ์œ ํšจํ•œ ์ขŒํ‘œ์ธ์ง€ ๋ฐฉ๋ฌธํ•œ ๊ณณ์ธ์ง€ ํ™•์ธ
if (
!isValidPosition(next.row, next.column) ||
matrix[next.row][next.column] === "#"
) {
// 4. ๋ฐฉํ–ฅ ์ „ํ™˜
directionIndex = (directionIndex + 1) % 4;
} else {
// 5. ๋ฐฉ๋ฌธ ํ‘œ์‹œ ํ›„ ๋‹ค์Œ ์ขŒํ‘œ๋กœ ์ด๋™
result.push(matrix[next.row][next.column]);
matrix[next.row][next.column] = "#";
current = next;
}
}

return result;

function isValidPosition(row, column) {
if (row < 0 || ROW <= row) {
return false;
}
if (column < 0 || COLUMN <= column) {
return false;
}
return true;
}
};
44 changes: 44 additions & 0 deletions valid-parentheses/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* TC: O(S)
* s ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธธ์ด๋งŒํผ ์ˆœํšŒ 1๋ฒˆ
*
* SC: O(S)
* ์ตœ์•…์˜ ๊ฒฝ์šฐ S์˜ ๊ธธ์ด๋งŒํผ stack ๋ฐฐ์—ด์— ๋ชจ๋‘ push ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
*
* S: s.length
*/

/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
const map = {
"(": ")",
"{": "}",
"[": "]",
};
const stack = [];

// 1. s์˜ ๊ธธ์ด๋งŒํผ ์ˆœํšŒ๋ฅผ ํ•˜๋ฉด์„œ
for (const char of s) {
// 2. ์—ด๋ฆฐ ๊ด„ํ˜ธ๋ผ๋ฉด stack์— ์ง์ง€์–ด์ง„ ๋‹ซํžŒ ๊ด„ํ˜ธ๋ฅผ ์ €์žฅ
// 3. ๋‹ซํžŒ ๊ด„ํ˜ธ๋ผ๋ฉด stack์—์„œ ๊บผ๋‚ธ ๊ฒƒ๊ณผ ๋™์ผํ•œ์ง€ ํ™•์ธ
switch (char) {
case "(":
case "{":
case "[":
stack.push(map[char]);
break;
case "}":
case ")":
case "]":
if (stack.pop() !== char) {
return false;
}
}
}

// 4. ๋‚จ์€ ๊ด„ํ˜ธ๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธ
return stack.length === 0;
};

0 comments on commit ce9ec5d

Please sign in to comment.