-
Notifications
You must be signed in to change notification settings - Fork 126
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
[gitsunmin] Week 6 Solutions #470
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
18150be
Add week 6 solutions: valid-parentheses
gitsunmin 21b5059
Add week 6 solutions: container-with-most-water
gitsunmin 8382887
Add week 6 solutions: design-add-and-search-words-data-structure
gitsunmin 36e7cf3
Add week 6 solutions: longest-increasing-subsequence
gitsunmin 36743b0
Add week 6 solutions: spiral-matrix
gitsunmin 20e96ce
Merge branch 'DaleStudy:main' into main
gitsunmin d531d5e
update comment on design-add-and-search-words-data-structure
gitsunmin 681a47d
update time complexity on longest-increasing-subsequence
gitsunmin 94720e1
Merge branch 'DaleStudy:main' into main
gitsunmin 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 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 @@ | ||
/** | ||
* https://leetcode.com/problems/container-with-most-water/ | ||
* time complexity : O(n) | ||
* space complexity : O(1) | ||
*/ | ||
export function maxArea(height: number[]): number { | ||
let s = 0, e = height.length - 1, max = 0; | ||
|
||
while (s < e) { | ||
max = Math.max((e - s) * Math.min(height[s], height[e]), max); | ||
if (height[s] < height[e]) s++; | ||
else e--; | ||
} | ||
return max; | ||
}; |
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,58 @@ | ||
/** | ||
* https://leetcode.com/problems/design-add-and-search-words-data-structure | ||
* n: The total number of words stored in the Trie. | ||
* m: The average length of each word. | ||
* | ||
* time complexity: | ||
* - addWord: O(m) | ||
* - search: O(26^m) in the worst case (with multiple wildcards) to O(m) in general cases. | ||
* space complexity: O(n * m) | ||
*/ | ||
class TrieNode { | ||
children: Map<string, TrieNode>; | ||
isEnd: boolean; | ||
|
||
constructor() { | ||
this.children = new Map(); | ||
this.isEnd = false; | ||
} | ||
} | ||
|
||
export class WordDictionary { | ||
private root: TrieNode; | ||
|
||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
|
||
addWord(word: string): void { | ||
let node = this.root; | ||
|
||
for (const 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 { | ||
return this.searchInNode(word, 0, this.root); | ||
} | ||
|
||
private searchInNode(word: string, index: number, node: TrieNode): boolean { | ||
if (index === word.length) return node.isEnd; | ||
|
||
const char = word[index]; | ||
|
||
if (char === '.') { | ||
for (const child of node.children.values()) { | ||
if (this.searchInNode(word, index + 1, child)) return true; | ||
} | ||
return false; | ||
} else { | ||
if (!node.children.has(char)) return false; | ||
return this.searchInNode(word, index + 1, node.children.get(char)!); | ||
} | ||
} | ||
} |
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,25 @@ | ||
/** | ||
* https://leetcode.com/problems/valid-parentheses/ | ||
* time complexity : O(n) | ||
* space complexity : O(n) | ||
*/ | ||
|
||
type OpeningBracket = '(' | '[' | '{'; | ||
type ClosingBracket = ')' | ']' | '}'; | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 타입스크립트의 강점을 십분 활용하고 계시는 군요 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다 🙏 |
||
|
||
const isEmpty = (stack: OpeningBracket[]): boolean => stack.length === 0; | ||
|
||
function isValid(s: string): boolean { | ||
const m = new Map<string, ClosingBracket>([ | ||
['(', ')'], | ||
['[', ']'], | ||
['{', '}'] | ||
]); | ||
const stack: OpeningBracket[] = []; | ||
|
||
for (const c of s) { | ||
if (m.has(c)) stack.push(c as OpeningBracket); | ||
else if (isEmpty(stack) || c !== m.get(stack.pop() as OpeningBracket)) return false; | ||
} | ||
return isEmpty(stack); | ||
}; |
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.
공간 복잡도도 메서드별로 개별적으로 평가 해 주시는 것이 좋지 않을까요?
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.
@HC-kang 아 넵 공간 복잡도는 두 메서드가 모두 같아서 하나로 작성했었는데, 두 개로 나누어 작성해둘게요