Skip to content

Commit

Permalink
Merge pull request #480 from JEONGHWANMIN/main
Browse files Browse the repository at this point in the history
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week6 ๋ฌธ์ œํ’€์ด
  • Loading branch information
JEONGHWANMIN authored Sep 22, 2024
2 parents 94da0df + f819319 commit c64a6d9
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
28 changes: 28 additions & 0 deletions container-with-most-water/hwanmini.js
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
68 changes: 68 additions & 0 deletions design-add-and-search-words-data-structure/hwanmini.js
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


66 changes: 66 additions & 0 deletions spiral-matrix/hwanmini.js
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]

39 changes: 39 additions & 0 deletions valid-parentheses/hwanmini.js
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

0 comments on commit c64a6d9

Please sign in to comment.