-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sukyoungshin] WEEK 06 #1445
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
Merged
Merged
[sukyoungshin] WEEK 06 #1445
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
// 1번째 풀이 | ||
function maxArea1(height: number[]): number { | ||
let left = 0; | ||
let right = height.length - 1; | ||
const area: number[] = []; | ||
|
||
while (left < right) { | ||
const x = right - left; | ||
const y = Math.min(height[left], height[right]); | ||
area.push(x * y); | ||
|
||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
|
||
return Math.max(...area); | ||
}; | ||
|
||
// 2번째 풀이 | ||
function maxArea2(height: number[]): number { | ||
let left = 0; | ||
let right = height.length - 1; | ||
let max = 0; | ||
|
||
while (left < right) { | ||
const x = right - left; | ||
const y = Math.min(height[left], height[right]); | ||
const current = x * y; | ||
max = Math.max(max, current); | ||
|
||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
|
||
return max; | ||
}; |
77 changes: 77 additions & 0 deletions
77
design-add-and-search-words-data-structure/sukyoungshin.ts
This file contains hidden or 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,77 @@ | ||
// 1번풀이 O(n × m) | ||
class WordDictionary1 { | ||
private words: string[]; | ||
constructor() { | ||
this.words = []; | ||
} | ||
|
||
addWord(word: string): void { | ||
this.words.push(word); | ||
} | ||
search(word: string): boolean { | ||
return this.words | ||
.filter((savedWord) => savedWord.length === word.length) | ||
.some((savedWord) => { | ||
for (let i = 0; i < word.length; i++) { | ||
if (word[i] === ".") continue; | ||
if (word[i] !== savedWord[i]) return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
}; | ||
|
||
// 2번풀이 : Trie(트라이) 자료구조 | ||
type TrieNode = { | ||
children: { [key: string]: TrieNode }; | ||
isEnd: boolean; | ||
}; | ||
|
||
class WordDictionary { | ||
private root: TrieNode; | ||
constructor() { | ||
this.root = { | ||
children: {}, | ||
isEnd: false, | ||
}; | ||
} | ||
addWord(word: string): void { | ||
let node = this.root; | ||
|
||
for (let i = 0; i < word.length; i++) { | ||
const char = word[i]; | ||
|
||
if (!node.children[char]) { | ||
node.children[char] = { | ||
children: {}, | ||
isEnd: false, | ||
}; | ||
} | ||
|
||
node = node.children[char]; | ||
} | ||
|
||
node.isEnd = true; | ||
} | ||
search(word: string): boolean { | ||
const dfs = (node: TrieNode, index: number): boolean => { | ||
if (index === word.length) return node.isEnd; | ||
|
||
const char = word[index]; | ||
|
||
if (char === ".") { | ||
for (const nextChar in node.children) { | ||
if (dfs(node.children[nextChar], index + 1)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
if (!node.children[char]) return false; | ||
return dfs(node.children[char], index + 1); | ||
}; | ||
|
||
return dfs(this.root, 0); | ||
} | ||
}; |
This file contains hidden or 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,24 @@ | ||
const pairs = { | ||
")": "(", | ||
"}": "{", | ||
"]": "[", | ||
}; | ||
|
||
function isValid(s: string): boolean { | ||
const stack: string[] = []; | ||
for (let i = 0; i < s.length; i++) { | ||
const str = s[i]; | ||
|
||
if (str in pairs) { | ||
if (pairs[str] !== stack[stack.length - 1]) { | ||
return false; | ||
} else { | ||
stack.pop(); | ||
} | ||
} else { | ||
stack.push(str); | ||
} | ||
} | ||
|
||
return stack.length === 0; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일반적으로 TRIE 자료조를 시작점으로 문제를 풀기 마련인데,
직접 (다른 방법으로) 답을 찾으신게 인상적이네요! 👍🏻👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trie 자료구조를 모르는 상태에서 어떻게든 풀어보려고 하다보니 남들이 하지 않는 삽질을 한번 더 하는거랍니다..🥹 좋게 봐주셔서 감사합니다!