-
-
Notifications
You must be signed in to change notification settings - Fork 245
[soobing] WEEK 06 Solutions #1425
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e8c532d
feat(soobing): week6 > valid-parentheses
soobing 0177065
feat(soobing): week6 > container-with-most-water
soobing 7c5ee41
feat(soobing): week6 > design-add-and-search-words-data-structure
soobing bfdf3b2
feat(soobing): week6 > longest-increasing-subsequence
soobing b807172
feat(soobing): week6 > spiral-matrix
soobing 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,33 @@ | ||
/** | ||
* | ||
* 문제 설명 | ||
* 가장 많이 담을 수 있는 물의 용기 구하기 | ||
* - height: 높이(n)를 담고 있는 배열 = y축 값 | ||
* - index: x축 값 | ||
* | ||
* 아이디어 | ||
* 1. 브루트포스 방식 O(n^2) | ||
* - 모든 쌍을 비교하여 최대 물의 양 찾기 | ||
* | ||
* 2. 투 포인터 방식 O(n) | ||
* - 왼쪽과 오른쪽 포인터를 이용하여 최대 물의 양 찾기 | ||
* - 같은 높이의 두 라인이 있는 경우 한쪽만 움직여도 최적의 해를 찾는데는 문제 없음 | ||
*/ | ||
function maxArea(height: number[]): number { | ||
let left = 0; | ||
let right = height.length - 1; | ||
let result = 0; | ||
while (left < right) { | ||
const x = right - left; | ||
const y = Math.min(height[left], height[right]); | ||
result = Math.max(x * y, result); | ||
|
||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
|
||
return result; | ||
} |
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,64 @@ | ||
/** | ||
* | ||
* 문제 설명 | ||
* - 문자열 추가/검색에 효율적인 데이터 구조 설계 | ||
* | ||
* 아이디어 | ||
* 1) 배열에 추가, 순차적으로 검색(.의 경우 정규식 사용) | ||
* - Time Limit Exceeded (TLE) 발생 | ||
* | ||
* 2) Trie 구조로 저장, 검색은 dfs | ||
* - 문자열 검색어 최적화 = Trie | ||
* | ||
*/ | ||
|
||
class TrieNode { | ||
children: Map<string, TrieNode> = new Map(); | ||
isEnd: boolean = false; | ||
} | ||
|
||
class WordDictionary { | ||
root: TrieNode; | ||
|
||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
|
||
addWord(word: string): void { | ||
let node = this.root; | ||
for (let char of word) { | ||
if (!node.children.has(char)) { | ||
node.children.set(char, new TrieNode()); | ||
} | ||
node = node.children.get(char)!; | ||
} | ||
node.isEnd = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
const dfs = (node: TrieNode, i: number) => { | ||
if (i === word.length) return node.isEnd; | ||
|
||
const char = word[i]; | ||
|
||
if (char === ".") { | ||
for (let child of node.children.values()) { | ||
if (dfs(child, i + 1)) return true; | ||
} | ||
return false; | ||
} else { | ||
const next = node.children.get(char); | ||
return next ? dfs(next, i + 1) : false; | ||
} | ||
}; | ||
|
||
return dfs(this.root, 0); | ||
} | ||
} | ||
|
||
/** | ||
* 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 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,15 @@ | ||
function isValid(s: string): boolean { | ||
const result: string[] = []; | ||
const open = ["(", "[", "{"]; | ||
const close = [")", "]", "}"]; | ||
for (let i = 0; i < s.length; i++) { | ||
if (open.includes(s[i])) { | ||
result.push(s[i]); | ||
} else { | ||
const index = close.findIndex((item) => item === s[i]); | ||
const popedItem = result.pop(); | ||
if (popedItem !== open[index]) return false; | ||
} | ||
} | ||
return result.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.
어떤 알고리즘을 활용할 수 있는지, 해당 알고리즘의 복잡도는 어떤지 분석한 아이디어를 적은 부분이 굉장히 좋은 것 같습니다.