-
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 #479 from whewchews/main
[pepper] Week 6 Solutions
- Loading branch information
Showing
4 changed files
with
146 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,24 @@ | ||
function maxArea(height: number[]): number { | ||
let left = 0; | ||
let right = height.length - 1; | ||
let maxSize = 0; | ||
|
||
while (left < right) { | ||
maxSize = Math.max(maxSize, getMaxSize(height, left, right)); | ||
|
||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
|
||
return maxSize; | ||
} | ||
|
||
function getMaxSize(height: number[], left: number, right: number) { | ||
return Math.min(...[height[right], height[left]]) * (right - left); | ||
} | ||
|
||
// TC: O(n) | ||
// SC: O(1) |
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,48 @@ | ||
class WordDictionary { | ||
wordCountMap: Map<number, Set<string>>; | ||
constructor() { | ||
this.wordCountMap = new Map(); | ||
} | ||
|
||
// TC: O(1) | ||
// SC: O(n) | ||
addWord(word: string): void { | ||
const length = word.length; | ||
if (this.wordCountMap.has(length)) { | ||
this.wordCountMap.get(length).add(word); | ||
} else { | ||
this.wordCountMap.set(length, new Set([word])); | ||
} | ||
return null; | ||
} | ||
|
||
// TC: O(m*n) // m: words length, n: word length | ||
// SC: O(n) | ||
search(word: string): boolean { | ||
const len = word.length; | ||
const targetWord = word.replace(/\./g, ""); | ||
const hasDot = len - targetWord.length !== 0; | ||
|
||
if (!this.wordCountMap.has(len)) { | ||
return false; | ||
} | ||
const words = this.wordCountMap.get(len); | ||
if (!hasDot) { | ||
return words.has(word); | ||
} | ||
|
||
for (const w of words) { | ||
let match = true; | ||
for (let j = 0; j < w.length; j++) { | ||
if (word[j] !== "." && word[j] !== w[j]) { | ||
match = false; | ||
break; | ||
} | ||
} | ||
if (match) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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,39 @@ | ||
function spiralOrder(matrix: number[][]): number[] { | ||
const rows = matrix.length; | ||
const cols = matrix[0].length; | ||
const total = rows * cols; | ||
let srow = 0; // start row | ||
let scol = 0; | ||
let erow = rows - 1; // end row | ||
let ecol = cols - 1; | ||
let count = 0; | ||
const ans: number[] = []; | ||
|
||
while (count < total) { | ||
for (let i = scol; i <= ecol && count < total; i++) { | ||
ans.push(matrix[srow][i]); | ||
count++; | ||
} | ||
srow++; | ||
for (let i = srow; i <= erow && count < total; i++) { | ||
ans.push(matrix[i][ecol]); | ||
count++; | ||
} | ||
ecol--; | ||
for (let i = ecol; i >= scol && count < total; i--) { | ||
ans.push(matrix[erow][i]); | ||
count++; | ||
} | ||
erow--; | ||
for (let i = erow; i >= srow && count < total; i--) { | ||
ans.push(matrix[i][scol]); | ||
count++; | ||
} | ||
scol++; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
// TC: O(m*n) | ||
// SC: O(m*n) |
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 @@ | ||
/* | ||
* ์์ด๋์ด | ||
* stack ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํด ์ฌ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด ์์๋๋ก ์ ์ฅํด๋๋ค. | ||
* stack์ ์ฌ์ฉํ๋ ์ด์ ๋ ๊ฐ์ฅ ์ต๊ทผ ์ฌ๋ ๊ดํธ๊ฐ ์ด๋ค ๊ฒ์ธ์ง ํ์ธํ๊ธฐ ์ํจ์ด๋ค.(๋ง์ง๋ง์ ์ฝ์ ํ ๊ฐ์ ๋จผ์ ์ฌ์ฉํจ) | ||
* ๋ซ๋ ๊ดํธ๊ฐ ๋์ค๋ฉด stack์ ๋ง์ง๋ง ๊ฐ์ด pair์ธ ์ฌ๋ ๊ดํธ์ธ์ง ์ฒดํฌํ๋ค. ๋ค๋ฅด๋ฉด return false | ||
* ๋ฌธ์์ด ๋ฐ๋ณต๋ฌธ์ ๋ค ๋๊ณ stack์ ์ฌ๋ ๊ดํธ๊ฐ ๋จ์์์ง ์์์ง ๋ณธ๋ค. ๋จ์์์ผ๋ฉด return false | ||
* ์์ ๋ ๊ฒฝ์ฐ์ ํด๋น๋์ง ์์ผ๋ฉด return true | ||
*/ | ||
function isValid(s: string): boolean { | ||
const openCharStack = []; | ||
const CHAR_PAIR = { | ||
"]": "[", | ||
"}": "{", | ||
")": "(", | ||
}; | ||
|
||
for (let i = 0; i <= s.length - 1; i++) { | ||
const char = s[i]; | ||
|
||
if (char in CHAR_PAIR) { | ||
const pair = CHAR_PAIR[char]; | ||
const lastOpenChar = openCharStack.pop(); | ||
if (lastOpenChar !== pair) { | ||
return false; | ||
} | ||
} else { | ||
openCharStack.push(char); | ||
} | ||
} | ||
|
||
const isEmpty = openCharStack.length === 0; | ||
return isEmpty; | ||
} | ||
// TC: O(n) | ||
// SC: O(n) |