Skip to content
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

[환미니니] Week6 문제풀이 #480

Merged
merged 4 commits into from
Sep 22, 2024
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
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