Skip to content

[sukyoungshin] WEEK 06 #1445

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

Merged
merged 3 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 42 additions & 0 deletions container-with-most-water/sukyoungshin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 1번째 풀이
function maxArea1(height: number[]): number {
let left = 0;
let right = height.length - 1;
const area: number[] = [];

while (left < right) {
const x = right - left;
const y = Math.min(height[left], height[right]);
area.push(x * y);

if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return Math.max(...area);
};

// 2번째 풀이
function maxArea2(height: number[]): number {
let left = 0;
let right = height.length - 1;
let max = 0;

while (left < right) {
const x = right - left;
const y = Math.min(height[left], height[right]);
const current = x * y;
max = Math.max(max, current);

if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return max;
};
77 changes: 77 additions & 0 deletions design-add-and-search-words-data-structure/sukyoungshin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 1번풀이 O(n × m)
class WordDictionary1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

일반적으로 TRIE 자료조를 시작점으로 문제를 풀기 마련인데,

직접 (다른 방법으로) 답을 찾으신게 인상적이네요! 👍🏻👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trie 자료구조를 모르는 상태에서 어떻게든 풀어보려고 하다보니 남들이 하지 않는 삽질을 한번 더 하는거랍니다..🥹 좋게 봐주셔서 감사합니다!

private words: string[];
constructor() {
this.words = [];
}

addWord(word: string): void {
this.words.push(word);
}
search(word: string): boolean {
return this.words
.filter((savedWord) => savedWord.length === word.length)
.some((savedWord) => {
for (let i = 0; i < word.length; i++) {
if (word[i] === ".") continue;
if (word[i] !== savedWord[i]) return false;
}
return true;
});
}
};

// 2번풀이 : Trie(트라이) 자료구조
type TrieNode = {
children: { [key: string]: TrieNode };
isEnd: boolean;
};

class WordDictionary {
private root: TrieNode;
constructor() {
this.root = {
children: {},
isEnd: false,
};
}
addWord(word: string): void {
let node = this.root;

for (let i = 0; i < word.length; i++) {
const char = word[i];

if (!node.children[char]) {
node.children[char] = {
children: {},
isEnd: false,
};
}

node = node.children[char];
}

node.isEnd = true;
}
search(word: string): boolean {
const dfs = (node: TrieNode, index: number): boolean => {
if (index === word.length) return node.isEnd;

const char = word[index];

if (char === ".") {
for (const nextChar in node.children) {
if (dfs(node.children[nextChar], index + 1)) {
return true;
}
}
return false;
}

if (!node.children[char]) return false;
return dfs(node.children[char], index + 1);
};

return dfs(this.root, 0);
}
};
24 changes: 24 additions & 0 deletions valid-parentheses/sukyoungshin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const pairs = {
")": "(",
"}": "{",
"]": "[",
};

function isValid(s: string): boolean {
const stack: string[] = [];
for (let i = 0; i < s.length; i++) {
const str = s[i];

if (str in pairs) {
if (pairs[str] !== stack[stack.length - 1]) {
return false;
} else {
stack.pop();
}
} else {
stack.push(str);
}
}

return stack.length === 0;
};