-
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 #480 from JEONGHWANMIN/main
[ํ๋ฏธ๋๋] Week6 ๋ฌธ์ ํ์ด
- Loading branch information
Showing
4 changed files
with
201 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,28 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
|
||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
var maxArea = function(height) { | ||
let maxArea = 0; | ||
|
||
let leftIdx = 0; | ||
let rightIdx = height.length - 1; | ||
|
||
while (leftIdx <= rightIdx) { | ||
const minHeight = Math.min(height[leftIdx], height[rightIdx]); | ||
const distance = rightIdx - leftIdx | ||
|
||
maxArea = Math.max(maxArea, distance * minHeight); | ||
|
||
if (height[leftIdx] < height[rightIdx]) leftIdx++ | ||
else rightIdx-- | ||
} | ||
|
||
return maxArea | ||
}; | ||
|
||
console.log(maxArea([1,8,6,2,5,4,8,3,7])) //49 | ||
console.log(maxArea([1,1])) //1 |
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,68 @@ | ||
// ์๊ฐ๋ณต์ก๋ | ||
// addWord: O(n) (n์ ์ถ๊ฐํ๋ ๋จ์ด์ ๊ธธ์ด) | ||
// search: O(m^n) (m์ ๊ฐ ๋ ธ๋์ ์ต๋ ์์ ์, n์ ๊ฒ์ํ๋ ๋จ์ด์ ๊ธธ์ด) | ||
|
||
// ๊ณต๊ฐ๋ณต์ก๋ | ||
// addWord: O(n) (n์ ์ถ๊ฐํ๋ ๋จ์ด์ ๊ธธ์ด) | ||
// search: O(n) (n์ ๊ฒ์ํ๋ ๋จ์ด์ ๊ธธ์ด, ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด) | ||
|
||
class TrieNode { | ||
constructor() { | ||
this.children = {} | ||
this.endOfWord = false | ||
} | ||
} | ||
|
||
class WordDictionary { | ||
constructor() { | ||
this.root = new TrieNode() | ||
} | ||
addWord(word) { | ||
let curNode = this.root | ||
|
||
for (let i = 0 ; i < word.length; i++) { | ||
if (!curNode.children[word[i]]) { | ||
curNode.children[word[i]] = new TrieNode(); | ||
} | ||
|
||
curNode = curNode.children[word[i]]; | ||
} | ||
|
||
curNode.endOfWord = true | ||
} | ||
search(word) { | ||
return this.searchInNode(word, this.root); | ||
} | ||
|
||
searchInNode(word, node) { | ||
let curNode = node; | ||
|
||
for (let i = 0; i < word.length; i++) { | ||
if (word[i] === '.') { | ||
for (let key in curNode.children) { | ||
if (this.searchInNode(word.slice(i + 1), curNode.children[key])) return true | ||
} | ||
return false | ||
} | ||
|
||
if (word[i] !== '.') { | ||
if (!curNode.children[word[i]]) return false | ||
curNode = curNode.children[word[i]]; | ||
} | ||
} | ||
|
||
return curNode.endOfWord | ||
} | ||
} | ||
|
||
|
||
const wordDictionary = new WordDictionary(); | ||
wordDictionary.addWord("bad"); | ||
wordDictionary.addWord("dad"); | ||
wordDictionary.addWord("mad"); | ||
console.log(wordDictionary.search("pad")); // return False | ||
console.log(wordDictionary.search("bad")); // return True | ||
console.log(wordDictionary.search(".ad")); // return True | ||
console.log(wordDictionary.search("b..")); // 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,66 @@ | ||
// ์๊ฐ๋ณต์ก๋ O(n * m) | ||
// ๊ณต๊ฐ๋ณต์ก๋ O(n * m) | ||
|
||
const isVisited = (matrix, row, col) => { | ||
return matrix[row][col] === '#' | ||
} | ||
|
||
/** | ||
* @param {number[][]} matrix | ||
* @return {number[]} | ||
*/ | ||
var spiralOrder = function(matrix) { | ||
const result = [] | ||
|
||
const colLen = matrix[0].length; | ||
const rowLen = matrix.length | ||
|
||
let direction = 'right' | ||
let row = 0; | ||
let col = 0; | ||
while (result.length < colLen * rowLen ) { | ||
result.push(matrix[row][col]) | ||
matrix[row][col] = '#' | ||
|
||
if (direction === 'right') { | ||
if (isVisited(matrix, row, col+1) || col === colLen - 1) { | ||
direction = 'down' | ||
row++ | ||
} else { | ||
col++ | ||
} | ||
} else if (direction === 'down') { | ||
if ((row + 1 < rowLen && isVisited(matrix, row+1, col) || row === rowLen - 1)) { | ||
direction = 'left' | ||
col-- | ||
} else { | ||
row++ | ||
} | ||
} else if (direction === 'left') { | ||
if ((col > 0 && isVisited(matrix, row, col-1) || col === 0)) { | ||
direction = 'up' | ||
row-- | ||
} else { | ||
col-- | ||
} | ||
} else if (direction === 'up') { | ||
if ( (row > 0 && isVisited(matrix,row-1, col))) { | ||
direction = 'right' | ||
col++ | ||
} else { | ||
row-- | ||
} | ||
} | ||
|
||
} | ||
|
||
return result | ||
}; | ||
|
||
|
||
|
||
console.log(spiralOrder([ | ||
[1,2,3], | ||
[4,5,6], | ||
[7,8,9]])) // [1,2,3,6,9,8,7,4,5] | ||
|
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,39 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isValid = function(s) { | ||
if (s.length % 2 !== 0) return false | ||
|
||
const stack = [] | ||
|
||
const opener = { | ||
"(" : ")", | ||
"{": "}", | ||
"[": "]" | ||
} | ||
|
||
for (let i = 0 ; i < s.length; i++) { | ||
if (s[i] in opener) { | ||
stack.push(s[i]); | ||
} else { | ||
if (opener[stack.at(-1)] === s[i]) { | ||
stack.pop() | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
|
||
return stack.length === 0 | ||
}; | ||
|
||
console.log(isValid("()")); // true | ||
console.log(isValid("()[]{}")); // true | ||
console.log(isValid("(]")); // false | ||
console.log(isValid("([])")); // true | ||
console.log(isValid("([}}])")); // true |