Skip to content

Commit 6dd8755

Browse files
authored
Merge pull request #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
+24
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)
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

+39
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

+35
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)