-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #462 from wogha95/main
[์ฌํธ] WEEK 06 Solutions
- Loading branch information
Showing
5 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |