-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #476 from Sunjae95/main
[선재] WEEK06 문제 풀이
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
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,28 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* 1. brute force -> time limited | ||
* 2. two pointer | ||
* | ||
* n: length of height | ||
* time complexity: O(n) | ||
* space complexity: O(1) | ||
*/ | ||
var maxArea = function (height) { | ||
let answer = 0; | ||
let start = 0; | ||
let end = height.length - 1; | ||
|
||
while (start !== end) { | ||
const w = end - start; | ||
const h = Math.min(height[start], height[end]); | ||
answer = Math.max(answer, w * h); | ||
if (height[start] >= height[end]) { | ||
end--; | ||
} else if (height[start] < height[end]) { | ||
start++; | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
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,35 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* 1. dfs -> time limited | ||
* 2. memoization + dfs | ||
* | ||
* n: length of nums | ||
* time complexity: O(n^2) | ||
* space complexity: O(n) | ||
*/ | ||
var lengthOfLIS = function (nums) { | ||
const memo = new Array(nums.length).fill(-1); | ||
let answer = 0; | ||
|
||
const dfs = (index) => { | ||
if (memo[index] !== -1) return memo[index]; | ||
|
||
let maxLength = 1; | ||
|
||
for (let i = index + 1; i < nums.length; i++) { | ||
if (nums[index] < nums[i]) { | ||
maxLength = Math.max(maxLength, 1 + dfs(i)); | ||
} | ||
} | ||
|
||
memo[index] = maxLength; | ||
return maxLength; | ||
}; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
answer = Math.max(answer, dfs(i)); | ||
} | ||
|
||
return answer; | ||
}; |
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,45 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* just implement question | ||
* | ||
* m: length of matrix | ||
* n: length of matrix[i] | ||
* time complexity: O(m * n) | ||
* space complexity: O(m * n) | ||
*/ | ||
var spiralOrder = function (matrix) { | ||
let count = matrix.length * matrix[0].length; | ||
let [r, c] = [1, 1]; | ||
const answer = []; | ||
const MAX_R = matrix.length + 2; | ||
const MAX_C = matrix[0].length + 2; | ||
const visited = Array.from({ length: MAX_R }, (_, i) => { | ||
return Array.from( | ||
{ length: MAX_C }, | ||
(_, j) => i === 0 || i === MAX_R - 1 || j === 0 || j === MAX_C - 1 | ||
); | ||
}); | ||
|
||
while (count--) { | ||
const check = { | ||
left: c >= 1 && visited[r][c - 1], | ||
right: c < MAX_C - 1 && visited[r][c + 1], | ||
top: r >= 1 && visited[r - 1][c], | ||
bottom: r < MAX_R - 1 && visited[r + 1][c], | ||
}; | ||
visited[r][c] = true; | ||
answer.push(matrix[r - 1][c - 1]); | ||
|
||
if (check.left && check.top && check.bottom) c++; | ||
else if (check.left && check.top && check.right) r++; | ||
else if (check.right && check.top && check.bottom) c--; | ||
else if (check.left && check.right && check.bottom) r--; | ||
else if (check.left && check.top) c++; | ||
else if (check.top && check.right) r++; | ||
else if (check.right && check.bottom) c--; | ||
else if (check.bottom && check.left) r--; | ||
} | ||
|
||
return answer; | ||
}; |
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,32 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* stack | ||
* | ||
* n: length of s | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var isValid = function (s) { | ||
const stack = []; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
if (s[i] === "(" || s[i] === "{" || s[i] === "[") { | ||
stack.push(s[i]); | ||
continue; | ||
} | ||
|
||
const top = stack.length ? stack[stack.length - 1] : null; | ||
if (top === null) return false; | ||
|
||
if ( | ||
(top === "(" && s[i] === ")") || | ||
(top === "{" && s[i] === "}") || | ||
(top === "[" && s[i] === "]") | ||
) { | ||
stack.pop(); | ||
} else return false; | ||
} | ||
|
||
return !stack.length; | ||
}; |