Skip to content
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

[pepper] Week 6 Solutions #479

Merged
merged 7 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions container-with-most-water/whewchews.ts
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)
48 changes: 48 additions & 0 deletions design-add-and-search-words-data-structure/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class WordDictionary {
wordList: Set<string>;
wordCountMap: Map<number, string[]>;
constructor() {
this.wordList = new Set();
this.wordCountMap = new Map();
}
whewchews marked this conversation as resolved.
Show resolved Hide resolved

// TC: O(1)
// SC: O(n)
addWord(word: string): void {
this.wordList.add(word);
const length = word.length;
if (this.wordCountMap.has(length)) {
this.wordCountMap.get(length).push(word);
} else {
this.wordCountMap.set(length, [word]);
}
return null;
}

// TC: O(m*n) // m: words length, n: word length
// SC: O(1)
search(word: string): boolean {
const len = word.length;
const targetWord = word.replace(/\./g, "");
whewchews marked this conversation as resolved.
Show resolved Hide resolved
const hasDot = len - targetWord.length !== 0;

if (!hasDot) return this.wordList.has(word);
if (!this.wordCountMap.has(len)) {
return false;
}
const words = this.wordCountMap.get(len);
for (let i = 0; i < words.length; i++) {
let match = true;
for (let j = 0; j < words[i].length; j++) {
if (word[j] !== "." && word[j] !== words[i][j]) {
match = false;
break;
}
}
if (match) {
return true;
}
}
return false;
}
}
39 changes: 39 additions & 0 deletions spiral-matrix/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function spiralOrder(matrix: number[][]): number[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

크 복잡한 조건이나 visited 같은 부수적인게 하나도 없는 깔끔한 코드네요 👍

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space complexity가 O(m*n)인 이유가 혹시 ans 배열 때문일까요?

보통 return하는 정답 객체는 공간 복잡도에 포함시키지 않는 것으로 알고 있습니다 :)

38 changes: 38 additions & 0 deletions valid-parentheses/whewchews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 아이디어
* stack 자료구조를 사용해 여는 괄호가 나오면 순서대로 저장해둔다.
* stack을 사용하는 이유는 가장 최근 여는 괄호가 어떤 것인지 확인하기 위함이다.(마지막에 삽입한 값을 먼저 사용함)
* 닫는 괄호가 나오면 stack의 마지막 값이 pair인 여는 괄호인지 체크한다. 다르면 return false
* 문자열 반복문을 다 돌고 stack에 여는 괄호가 남아있지 않은지 본다. 남아있으면 return false
* 위의 두 경우에 해당되지 않으면 return true
*/
function isValid(s: string): boolean {
const charSet = new Set(["(", "{", "["]);
const openCharStack = [];
const CHAR_PAIR = {
"]": "[",
"}": "{",
")": "(",
};

for (let i = 0; i <= s.length - 1; i++) {
const char = s[i];
if (charSet.has(char)) {
openCharStack.push(char);
continue;
}

if (char in CHAR_PAIR) {
const pair = CHAR_PAIR[char];
const lastOpenChar = openCharStack.pop();
if (lastOpenChar !== pair) {
return false;
}
Comment on lines +22 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stack이 비어 있는 경우도 처리해주어야 하지 않나 싶었는데, JS에선 빈 배열을 pop할 때 undefined를 반환한다는 사실 덕분에 알아 갑니다 ㅎㅎ

}
}
whewchews marked this conversation as resolved.
Show resolved Hide resolved

const isEmpty = openCharStack.length === 0;
return isEmpty;
}
// TC: O(n)
// SC: O(n)