Skip to content

Commit 6dd8755

Browse files
authored
Merge pull request DaleStudy#479 from whewchews/main
[pepper] Week 6 Solutions
2 parents 4109f2e + 28876ed commit 6dd8755

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function maxArea(height: number[]): number {
2+
let left = 0;
3+
let right = height.length - 1;
4+
let maxSize = 0;
5+
6+
while (left < right) {
7+
maxSize = Math.max(maxSize, getMaxSize(height, left, right));
8+
9+
if (height[left] < height[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
16+
return maxSize;
17+
}
18+
19+
function getMaxSize(height: number[], left: number, right: number) {
20+
return Math.min(...[height[right], height[left]]) * (right - left);
21+
}
22+
23+
// TC: O(n)
24+
// SC: O(1)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class WordDictionary {
2+
wordCountMap: Map<number, Set<string>>;
3+
constructor() {
4+
this.wordCountMap = new Map();
5+
}
6+
7+
// TC: O(1)
8+
// SC: O(n)
9+
addWord(word: string): void {
10+
const length = word.length;
11+
if (this.wordCountMap.has(length)) {
12+
this.wordCountMap.get(length).add(word);
13+
} else {
14+
this.wordCountMap.set(length, new Set([word]));
15+
}
16+
return null;
17+
}
18+
19+
// TC: O(m*n) // m: words length, n: word length
20+
// SC: O(n)
21+
search(word: string): boolean {
22+
const len = word.length;
23+
const targetWord = word.replace(/\./g, "");
24+
const hasDot = len - targetWord.length !== 0;
25+
26+
if (!this.wordCountMap.has(len)) {
27+
return false;
28+
}
29+
const words = this.wordCountMap.get(len);
30+
if (!hasDot) {
31+
return words.has(word);
32+
}
33+
34+
for (const w of words) {
35+
let match = true;
36+
for (let j = 0; j < w.length; j++) {
37+
if (word[j] !== "." && word[j] !== w[j]) {
38+
match = false;
39+
break;
40+
}
41+
}
42+
if (match) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
}

โ€Žspiral-matrix/whewchews.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function spiralOrder(matrix: number[][]): number[] {
2+
const rows = matrix.length;
3+
const cols = matrix[0].length;
4+
const total = rows * cols;
5+
let srow = 0; // start row
6+
let scol = 0;
7+
let erow = rows - 1; // end row
8+
let ecol = cols - 1;
9+
let count = 0;
10+
const ans: number[] = [];
11+
12+
while (count < total) {
13+
for (let i = scol; i <= ecol && count < total; i++) {
14+
ans.push(matrix[srow][i]);
15+
count++;
16+
}
17+
srow++;
18+
for (let i = srow; i <= erow && count < total; i++) {
19+
ans.push(matrix[i][ecol]);
20+
count++;
21+
}
22+
ecol--;
23+
for (let i = ecol; i >= scol && count < total; i--) {
24+
ans.push(matrix[erow][i]);
25+
count++;
26+
}
27+
erow--;
28+
for (let i = erow; i >= srow && count < total; i--) {
29+
ans.push(matrix[i][scol]);
30+
count++;
31+
}
32+
scol++;
33+
}
34+
35+
return ans;
36+
}
37+
38+
// TC: O(m*n)
39+
// SC: O(m*n)

โ€Žvalid-parentheses/whewchews.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* ์•„์ด๋””์–ด
3+
* stack ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ค๋ฉด ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•ด๋‘”๋‹ค.
4+
* stack์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์–ด๋–ค ๊ฒƒ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.(๋งˆ์ง€๋ง‰์— ์‚ฝ์ž…ํ•œ ๊ฐ’์„ ๋จผ์ € ์‚ฌ์šฉํ•จ)
5+
* ๋‹ซ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ค๋ฉด stack์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด pair์ธ ์—ฌ๋Š” ๊ด„ํ˜ธ์ธ์ง€ ์ฒดํฌํ•œ๋‹ค. ๋‹ค๋ฅด๋ฉด return false
6+
* ๋ฌธ์ž์—ด ๋ฐ˜๋ณต๋ฌธ์„ ๋‹ค ๋Œ๊ณ  stack์— ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚จ์•„์žˆ์ง€ ์•Š์€์ง€ ๋ณธ๋‹ค. ๋‚จ์•„์žˆ์œผ๋ฉด return false
7+
* ์œ„์˜ ๋‘ ๊ฒฝ์šฐ์— ํ•ด๋‹น๋˜์ง€ ์•Š์œผ๋ฉด return true
8+
*/
9+
function isValid(s: string): boolean {
10+
const openCharStack = [];
11+
const CHAR_PAIR = {
12+
"]": "[",
13+
"}": "{",
14+
")": "(",
15+
};
16+
17+
for (let i = 0; i <= s.length - 1; i++) {
18+
const char = s[i];
19+
20+
if (char in CHAR_PAIR) {
21+
const pair = CHAR_PAIR[char];
22+
const lastOpenChar = openCharStack.pop();
23+
if (lastOpenChar !== pair) {
24+
return false;
25+
}
26+
} else {
27+
openCharStack.push(char);
28+
}
29+
}
30+
31+
const isEmpty = openCharStack.length === 0;
32+
return isEmpty;
33+
}
34+
// TC: O(n)
35+
// SC: O(n)

0 commit comments

Comments
ย (0)