-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Jeehay28] WEEK06 Solutions #1429
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
Changes from all commits
661204e
b13d233
079e214
eaed2a9
1cf59f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// TC: O(n) | ||
// SC: O(1) | ||
|
||
function maxArea(height: number[]): number { | ||
let left = 0; | ||
let right = height.length - 1; | ||
let maxWater = 0; | ||
|
||
while (left < right) { | ||
const x = right - left; | ||
const y = Math.min(height[left], height[right]); | ||
|
||
maxWater = Math.max(x * y, maxWater); | ||
|
||
if (height[left] <= height[right]) { | ||
left += 1; | ||
} else { | ||
right -= 1; | ||
} | ||
} | ||
|
||
return maxWater; | ||
} | ||
|
||
|
||
// ❌ Time Limit Exceeded! | ||
// TC: O(n^2) | ||
|
||
// function maxArea(height: number[]): number { | ||
// // input: an integer array -> height | ||
// // output: the maximum amount of water | ||
// // height = [1, 8, 6, 2, 5, 4, 8, 3, 7] | ||
|
||
// let maxWater = 0; | ||
|
||
// for (let start = 0; start < height.length - 1; start++) { | ||
// for (let end = start + 1; end < height.length; end++) { | ||
// const x = end - start; | ||
// const y = Math.min(height[start], height[end]); | ||
// maxWater = Math.max(maxWater, x * y); | ||
// } | ||
// } | ||
|
||
// return maxWater; | ||
// } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
class WordDictionary { | ||
root: { | ||
[key: string]: any; | ||
}; | ||
|
||
constructor() { | ||
this.root = { $: true }; // ending | ||
} | ||
|
||
// { | ||
// "$": true, | ||
// "b": { | ||
// "$": false, | ||
// "a": { | ||
// "$": false, | ||
// "d": { | ||
// "$": true | ||
// } | ||
// } | ||
// } | ||
// } | ||
|
||
// TC: O(w) | ||
// SC: O(w) | ||
addWord(word: string): void { | ||
let node = this.root; | ||
|
||
for (const ch of word) { | ||
if (!node[ch]) { | ||
node[ch] = { $: false }; | ||
} | ||
node = node[ch]; | ||
} | ||
|
||
node["$"] = true; | ||
} | ||
|
||
// TC: O(26^w) | ||
// SC: O(w) | ||
search(word: string): boolean { | ||
const dfs = (node, idx: number) => { | ||
if (idx === word.length) { | ||
return node["$"]; | ||
} | ||
|
||
const ch = word[idx]; | ||
|
||
if (node[ch]) { | ||
return dfs(node[ch], idx + 1); | ||
} | ||
|
||
if (ch === ".") { | ||
for (const key of Object.keys(node)) { | ||
if (key !== "$" && dfs(node[key], idx + 1)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return 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) | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Time Complexity: O(n * log n) | ||
// Space Complexity: O(n) | ||
|
||
function lengthOfLIS(nums: number[]): number { | ||
const sub: number[] = []; // O(n) | ||
|
||
for (const num of nums) { // O(n) | ||
// lower bound binary search: find the first index where element >= target | ||
let left = 0; | ||
let right = sub.length; | ||
const target = num; | ||
|
||
while (left < right) { // O(log n) | ||
let mid = Math.floor((left + right) / 2); | ||
if (sub[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
|
||
// if target is greater than all elements in sub | ||
if (left === sub.length) { | ||
sub.push(target); | ||
} else { | ||
// replace the first element >= target | ||
sub[left] = target; | ||
} | ||
} | ||
|
||
return sub.length; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// TC: O(m * n) | ||
// SC: O(1), if excluding the out array | ||
|
||
function spiralOrder(matrix: number[][]): number[] { | ||
let n_rows = matrix.length; | ||
let n_cols = matrix[0].length; | ||
let row = 0; | ||
let col = -1; | ||
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. 근데 col은 시작값이 왜 -1인지 알 수 있을까요?! 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. 그렇게 해야 반복문 최초 시작 시에 col = 0으로 시작되어요. 즉, col 값을 0으로 시작하기 위한 보정이라고 볼 수 있습니다. |
||
let direction = 1; | ||
|
||
const output: number[] = []; | ||
|
||
while (n_rows > 0 && n_cols > 0) { | ||
// move horizontally: right or left | ||
for (let i = 0; i < n_cols; i++) { | ||
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. 달래님 해설 강의에서 봤습니다. 😊 |
||
col += direction; // in first iteration, direction = 1 | ||
output.push(matrix[row][col]); | ||
} | ||
n_rows -= 1; | ||
|
||
// move vertically: down or up | ||
for (let j = 0; j < n_rows; j++) { | ||
row += direction; | ||
output.push(matrix[row][col]); | ||
} | ||
n_cols -= 1; | ||
|
||
// change direction | ||
direction *= -1; | ||
} | ||
|
||
return output; | ||
} | ||
|
||
|
||
// TC: O(m * n) | ||
// SC: O(1), if excluding the out array | ||
|
||
// function spiralOrder(matrix: number[][]): number[] { | ||
// // move: right -> down -> left -> up -> right -> ... | ||
// // matrix = [[1,2,3],[4,5,6],[7,8,9]] | ||
|
||
// let top = 0; | ||
// let bottom = matrix.length - 1; | ||
// let left = 0; | ||
// let right = matrix[0].length - 1; | ||
// let result: number[] = []; | ||
|
||
// while (top <= bottom && left <= right) { | ||
// // to the right | ||
// for (let col = left; col <= right; col++) { | ||
// result.push(matrix[top][col]); | ||
// } | ||
// top += 1; | ||
|
||
// // down | ||
// for (let row = top; row <= bottom; row++) { | ||
// result.push(matrix[row][right]); | ||
// } | ||
// right -= 1; | ||
|
||
// // to the left | ||
// // check needed because top was updated above | ||
// if (top <= bottom) { | ||
// for (let col = right; col >= left; col--) { | ||
// result.push(matrix[bottom][col]); | ||
// } | ||
// bottom -= 1; | ||
// } | ||
|
||
// // up | ||
// // check needed because right was updated above | ||
// if (left <= right) { | ||
// for (let row = bottom; row >= top; row--) { | ||
// result.push(matrix[row][left]); | ||
// } | ||
// left += 1; | ||
// } | ||
// } | ||
|
||
// return result; | ||
// } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Time Complexity: O(n) | ||
// Space Complexity: O(n) | ||
|
||
function isValid(s: string): boolean { | ||
const map = new Map<string, string>([ | ||
["(", ")"], | ||
["{", "}"], | ||
["[", "]"], | ||
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. 키 타입이 단순하고 문자열뿐이기 때문에 new Map() 객체대신 객체 리터럴을 사용하면 더 간단하고 빠를 것 같습니다! |
||
]); | ||
|
||
let stack: string[] = []; | ||
|
||
for (const ch of s) { | ||
if (map.has(ch)) { | ||
stack.push(ch); | ||
} else { | ||
const last = stack.pop(); | ||
if (!last || ch !== map.get(last)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return stack.length === 0; | ||
} | ||
|
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.
👍🏻