Skip to content

Commit

Permalink
Merge pull request #479 from whewchews/main
Browse files Browse the repository at this point in the history
[pepper] Week 6 Solutions
  • Loading branch information
SamTheKorean authored Sep 22, 2024
2 parents 4109f2e + 28876ed commit 6dd8755
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
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 {
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;
}
}
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[] {
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)
35 changes: 35 additions & 0 deletions valid-parentheses/whewchews.ts
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)

0 comments on commit 6dd8755

Please sign in to comment.