-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ganu] Week6 #886
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
[ganu] Week6 #886
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
99aa78a
feat: 20. Valid Parentheses
gwbaik9717 d0368fa
feat: 11. Container With Most Water
gwbaik9717 3f2b2ec
feat: 211. Design Add and Search Words Data Structure
gwbaik9717 93a584f
feat: 300. Longest Increasing Subsequence
gwbaik9717 8366313
feat: 54. Spiral Matrix
gwbaik9717 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,29 @@ | ||
// n: len(height) | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
var maxArea = function (height) { | ||
const n = height.length; | ||
let left = 0; | ||
let right = n - 1; | ||
let answer = 0; | ||
|
||
while (left < right) { | ||
const w = right - left; | ||
const h = Math.min(height[left], height[right]); | ||
|
||
answer = Math.max(w * h, answer); | ||
|
||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
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,67 @@ | ||
var Node = function () { | ||
this.children = new Map(); | ||
this.isEnd = false; | ||
}; | ||
|
||
var WordDictionary = function () { | ||
this.root = new Node(); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
WordDictionary.prototype.addWord = function (word) { | ||
let current = this.root; | ||
|
||
for (const chr of word) { | ||
if (!current.children.has(chr)) { | ||
current.children.set(chr, new Node()); | ||
} | ||
|
||
current = current.children.get(chr); | ||
} | ||
|
||
current.isEnd = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
WordDictionary.prototype.search = function (word) { | ||
const stack = [[0, this.root]]; | ||
|
||
while (stack.length > 0) { | ||
const [index, currentNode] = stack.pop(); | ||
|
||
if (index === word.length) { | ||
if (currentNode.isEnd) { | ||
return true; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if (word[index] === ".") { | ||
for (const [_, child] of currentNode.children) { | ||
stack.push([index + 1, child]); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if (currentNode.children.has(word[index])) { | ||
stack.push([index + 1, currentNode.children.get(word[index])]); | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
/** | ||
* 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,21 @@ | ||
// n: len(nums) | ||
// Time complexity: O(n^2) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var lengthOfLIS = function (nums) { | ||
const dp = Array.from({ length: nums.length }, () => 1); | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (nums[j] < nums[i]) { | ||
dp[i] = Math.max(dp[i], dp[j] + 1); | ||
} | ||
} | ||
} | ||
|
||
return Math.max(...dp); | ||
}; |
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,52 @@ | ||
// n: height of matrix, m: width of matrix | ||
// Time complexity: O(n*m) | ||
// Space complexity: O(n+m) | ||
|
||
/** | ||
* @param {number[][]} matrix | ||
* @return {number[]} | ||
*/ | ||
var spiralOrder = function (matrix) { | ||
const n = matrix.length; | ||
const m = matrix[0].length; | ||
|
||
// order of direction: East, South, West, North | ||
const dy = [0, 1, 0, -1]; | ||
const dx = [1, 0, -1, 0]; | ||
|
||
let dir = 0; | ||
let y = 0; | ||
let x = 0; | ||
|
||
const answer = []; | ||
|
||
while (true) { | ||
answer.push(matrix[y][x]); | ||
matrix[y][x] = ""; | ||
|
||
let ny = y + dy[dir]; | ||
let nx = x + dx[dir]; | ||
|
||
if (ny >= 0 && ny < n && nx >= 0 && nx < m && matrix[ny][nx] !== "") { | ||
y = ny; | ||
x = nx; | ||
continue; | ||
} | ||
|
||
// If the new position is out of bounds or already visited, Change direction | ||
dir = (dir + 1) % 4; | ||
|
||
ny = y + dy[dir]; | ||
nx = x + dx[dir]; | ||
|
||
// If the changed direction still has a problem, Break the loop | ||
if (ny < 0 || ny >= n || nx < 0 || nx >= m || matrix[ny][nx] === "") { | ||
break; | ||
} | ||
|
||
y = ny; | ||
x = nx; | ||
} | ||
|
||
return answer; | ||
}; |
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,26 @@ | ||
// n: len(s) | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isValid = function (s) { | ||
const stack = []; | ||
|
||
for (const chr of s) { | ||
if ( | ||
(stack.at(-1) === "(" && chr === ")") || | ||
(stack.at(-1) === "{" && chr === "}") || | ||
(stack.at(-1) === "[" && chr === "]") | ||
) { | ||
stack.pop(); | ||
continue; | ||
} | ||
|
||
stack.push(chr); | ||
} | ||
|
||
return stack.length === 0; | ||
}; |
Oops, something went wrong.
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.
혹시 공간복잡도를 이렇게 판단하신 이유가 있으실까요?
answer
배열을 포함하셨다면O(n*m)
이 되어야 할 것 같고, 제외한다면O(1)
으로도 표현할 수 있을 것 같아서요!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.
말씀해주신 것처럼
answer
배열을 포함을 해서O(n*m)
으로 판단하였습니다! 어디까지 공간 복잡도로 포함시켜야할지 기준이 약간 모호하다 느껴서요, 현재 저는 input 을 제외한 나머지를 모두 공간 복잡도에 포함시키고 있습니다.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.
아하 그렇다면 Space complexity를
O(n+m)
로 기입해주신 부분은 오타일까요..?!