Skip to content

Commit e7df407

Browse files
authored
Merge pull request DaleStudy#899 from mmyeon/main
[mallayon] Week 6
2 parents bf24cec + 17ec148 commit e7df407

File tree

5 files changed

+317
-0
lines changed

5 files changed

+317
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
*@link https://leetcode.com/problems/container-with-most-water/description/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - ๊ฐ€์žฅ ๋งŽ์€ ๋ฌผ์˜ ์–‘์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ํˆฌ ํฌ์ธํ„ฐ ์‚ฌ์šฉ
6+
* - ํ•œ ๋†’์ด๊ฐ€ ๋‚ฎ์œผ๋ฉด ๋‹ค๋ฅธ ๋†’์ด๊ฐ€ ์•„๋ฌด๋ฆฌ ๋†’์•„๋„ ์˜๋ฏธ๊ฐ€ ์—†์–ด์„œ, ์ž‘์€ ๋†’์ด๋ฅผ ๊ฐ€์ง„ ํฌ์ธํ„ฐ ์ด๋™ํ•˜๋ฉฐ ๊ณ„์‚ฐ
7+
* - ์–‘ ๋์—์„œ ํฌ์ธํ„ฐ ์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค ์ตœ๋Œ€๊ฐ’ ๊ฐฑ์‹ 
8+
*
9+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
10+
* - ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๋ฐฐ์—ด ์–‘ ๋์—์„œ 1๋ฒˆ์”ฉ ์ด๋™ํ•˜๋ฏ€๋กœ
11+
*
12+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(1)
13+
* - ํฌ์ธํ„ฐ(left,right)์™€ ์ตœ๋Œ€๊ฐ’ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉ
14+
*/
15+
function maxArea(height: number[]): number {
16+
let left = 0,
17+
right = height.length - 1,
18+
maxWater = 0;
19+
20+
while (left < right) {
21+
const width = right - left;
22+
const minHeight = Math.min(height[left], height[right]);
23+
maxWater = Math.max(maxWater, width * minHeight);
24+
25+
if (height[left] < height[right]) {
26+
left++;
27+
} else {
28+
right--;
29+
}
30+
}
31+
32+
return maxWater;
33+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
*@link https://leetcode.com/problems/design-add-and-search-words-data-structure/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - set ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
6+
* - ๋‹จ์–ด ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” set์— ์ €์žฅํ•˜๊ณ , ๋‹จ์–ด ์ฐพ์„ ๋•Œ๋งˆ๋‹ค cache๋ชฉ๋ก์—์„œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ.
7+
* - ๋‹จ์–ด ์ถ”๊ฐ€ํ•˜๋ฉด ์ด์ „ ์บ์‹œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋‹ˆ๊นŒ ํด๋ฆฌ์–ด ํ•ด์ฃผ๊ธฐ
8+
* - ์ด์ „ ๊ฐ’ ํ™œ์šฉํ•œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ ๋‹จ์–ด ์ถ”๊ฐ€์‹œ ๋‹ค์‹œ ์บ์‹œ ๋งŒ๋“ค์–ด์•ผํ•˜๋Š” ๋น„ํšจ์œจ ์กด์žฌ
9+
*
10+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n * k)
11+
* - n์€ set์— ์ถ”๊ฐ€๋œ ๋‹จ์–ด ๊ธธ์ด, k๋Š” ๊ฐ ๋‹จ์–ด ํ‰๊ท  ๊ธธ์ด
12+
* - this.set ๋‹จ์–ด ์ˆœํšŒ => O(n)
13+
* - replaceAll, filter => O(k)
14+
*
15+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n * k)
16+
* - n์€ ํŒจํ„ด์˜ ๊ฐœ์ˆ˜, k๋Š” ํŒจํ„ด์— ๋งค์นญ๋˜๋Š” ํ‰๊ท  ๋‹จ์–ด ์ˆ˜
17+
*/
18+
class WordDictionary {
19+
set: Set<string>;
20+
cache: Map<string, string[]>;
21+
constructor() {
22+
this.set = new Set();
23+
this.cache = new Map();
24+
}
25+
26+
addWord(word: string): void {
27+
this.set.add(word);
28+
// ์ƒˆ๋กœ์šด ๋‹จ์–ด๊ฐ€ ์ถ”๊ฐ€ ์‹œ ์บ์‹œ ํด๋ฆฌ์–ด
29+
this.cache.clear();
30+
}
31+
32+
search(word: string): boolean {
33+
// ์บ์‹œ์— ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ์ฆ‰์‹œ ๋ฆฌํ„ด
34+
if (this.cache.has(word)) return this.cache.get(word)!.length > 0;
35+
36+
// ์ •๊ทœํ‘œํ˜„์‹ ํŒจํ„ด ์ƒ์„ฑ
37+
const pattern = new RegExp(`^${word.replaceAll(".", "[a-z]")}$`);
38+
const matches = [...this.set].filter((item) => pattern.test(item));
39+
40+
// ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์บ์‹œ์— ์ €์žฅ
41+
this.cache.set(word, matches);
42+
43+
return matches.length > 0;
44+
}
45+
}
46+
47+
/**
48+
* Your WordDictionary object will be instantiated and called as such:
49+
* var obj = new WordDictionary()
50+
* obj.addWord(word)
51+
* var param_2 = obj.search(word)
52+
*/
53+
54+
// ์‹œ๊ฐ„ ๋ณต์žก๋„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ Trie ์ž๋ฃŒ ๊ตฌ์กฐ ์‚ฌ์šฉ
55+
class TrieNode {
56+
children: Map<string, TrieNode>;
57+
isEndOfWord: boolean;
58+
59+
constructor() {
60+
this.children = new Map();
61+
this.isEndOfWord = false;
62+
}
63+
}
64+
65+
/*
66+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
67+
* - ๊ฐ ๋ฌธ์ž ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ Trie ๊ตฌ์กฐ ์‚ฌ์šฉ
68+
* - ์™€์ผ๋“œ์นด๋“œ(.)๊ฐ€ ํฌํ•จ๋œ ๋‹จ์–ด๋Š” ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์žฌ๊ท€์ ์œผ๋กœ ํ™•์ธ
69+
*
70+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n * k)
71+
* - n : ๋‹จ์–ด ๊ฐœ์ˆ˜, m : ๋‹จ์–ด ํ‰๊ท  ๊ธธ์ด => addWord์˜ ๋ณต์žก๋„ : O(n * m)
72+
* - c : ์ž์‹๋…ธ๋“œ ๊ฐœ์ˆ˜, d : ์™€์ผ๋“œ์นด๋“œ ๊ฐœ์ˆ˜, m : ๋‹จ์–ด ๊ธธ์ด => search์˜ ๋ณต์žก๋„ : O(c^d * m)
73+
* - ์™€์ผ๋“œ ์นด๋“œ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐ ๋…ธ๋“œ ๋ณ„ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•œ๋‹ค.
74+
*
75+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n * k)
76+
* - n์€ ํŒจํ„ด์˜ ๊ฐœ์ˆ˜, k๋Š” ํŒจํ„ด์— ๋งค์นญ๋˜๋Š” ํ‰๊ท  ๋‹จ์–ด ์ˆ˜
77+
*/
78+
class WordDictionary {
79+
root: TrieNode;
80+
constructor() {
81+
this.root = new TrieNode();
82+
}
83+
84+
addWord(word: string): void {
85+
let currentNode = this.root;
86+
87+
for (const letter of word) {
88+
if (!currentNode.children.has(letter))
89+
currentNode.children.set(letter, new TrieNode());
90+
currentNode = currentNode.children.get(letter)!;
91+
}
92+
93+
currentNode.isEndOfWord = true;
94+
}
95+
96+
search(word: string): boolean {
97+
const dfs = (currentNode: TrieNode, index: number): boolean => {
98+
if (index === word.length) return currentNode.isEndOfWord;
99+
100+
const char = word[index];
101+
// ์™€์ผ๋“œ ์นด๋“œ์ธ ๊ฒฝ์šฐ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ ์ˆœํšŒํ•œ๋‹ค
102+
if (char === ".") {
103+
// ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ ์ˆœํšŒ
104+
for (const key of currentNode.children.keys()) {
105+
if (dfs(currentNode.children.get(key)!, index + 1)) return true;
106+
}
107+
return false;
108+
} else {
109+
if (!currentNode.children.has(char)) return false;
110+
return dfs(currentNode.children.get(char)!, index + 1);
111+
}
112+
};
113+
114+
return dfs(this.root, 0);
115+
}
116+
}
117+
118+
/**
119+
* Your WordDictionary object will be instantiated and called as such:
120+
* var obj = new WordDictionary()
121+
* obj.addWord(word)
122+
* var param_2 = obj.search(word)
123+
*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
*
3+
* @link https://leetcode.com/problems/longest-increasing-subsequence/description/
4+
*
5+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• : dp ์‚ฌ์šฉ
6+
* - dp[i]์— i๋ฒˆ์งธ ์š”์†Œ ํฌํ•จํ•œ LIS ๊ธธ์ด ์ €์žฅ
7+
* - ํ˜„์žฌ ๊ฐ’์ด ์ด์ „๊ฐ’๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ, ์ด์ „๊ฐ’ ์ˆœํšŒํ•˜๋ฉด์„œ LIS ์—…๋ฐ์ดํŠธํ•˜๊ธฐ
8+
*
9+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n^2)
10+
* - ๊ฐ nums ์ˆœํšŒํ•˜๋ฉด์„œ ์ด์ „๊ฐ’ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด ์ˆœํšŒํ•˜๋‹ˆ๊นŒ O(n^2)
11+
*
12+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
13+
* - nums ๊ธธ์ด๋งŒํผ dp ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๋‹ˆ๊นŒ O(n)
14+
*/
15+
16+
function lengthOfLIS(nums: number[]): number {
17+
const dp = Array(nums.length).fill(1);
18+
19+
for (let i = 1; i < nums.length; i++) {
20+
for (let j = 0; j < i; j++) {
21+
if (nums[j] < nums[i]) dp[i] = Math.max(dp[i], dp[j] + 1);
22+
}
23+
}
24+
25+
return Math.max(...dp);
26+
}
27+
28+
/**
29+
*
30+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• : ์ด์ง„ ํƒ์ƒ‰ ์‚ฌ์šฉ
31+
* - num ์‚ฝ์ž…ํ•  ์ธ๋ฑ์Šค ์ฐพ๊ธฐ ์œ„ํ•ด์„œ ์ด์ง„ ํƒ์ƒ‰ ์‚ฌ์šฉ
32+
* - nums ๋ฐฐ์—ด ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ num์˜ ์œ„์น˜ ์ฐพ๊ธฐ
33+
* - ์œ„์น˜๊ฐ€ ๋ฐฐ์—ด ๋‚ด์— ์กด์žฌํ•˜๋ฉด ๊ธฐ์กด ๊ฐ’์„ ๋Œ€์ฒดํ•˜๊ณ , ์œ„์น˜๊ฐ€ ๋ฐฐ์—ด ๊ธธ์ด๋ณด๋‹ค ํฌ๋ฉด ๋ฐฐ์—ด์— ์ถ”๊ฐ€
34+
*
35+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(nlogn)
36+
* - nums ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ด์ง„ ํƒ์ƒ‰ O(nlogn)
37+
*
38+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
39+
* - arr์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๊ฐ€ nums ๊ธธ์ด๋งŒํผ ํ•„์š”
40+
*/
41+
42+
function lengthOfLIS(nums: number[]): number {
43+
const arr: number[] = [];
44+
45+
nums.forEach((num) => {
46+
let left = 0,
47+
right = arr.length;
48+
// num ์‚ฝ์ž…ํ•  ์œ„์น˜๋ฅผ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ์ฐพ์Œ
49+
while (left < right) {
50+
const mid = Math.floor((left + right) / 2);
51+
52+
if (arr[mid] < num) {
53+
left = mid + 1;
54+
} else right = mid;
55+
}
56+
57+
// ๊ธฐ์กด ๊ฐ’ ๋Œ€์ฒด
58+
if (left < arr.length) arr[left] = num;
59+
// ์ƒˆ๋กœ์šด ๊ฐ’ ์ถ”๊ฐ€
60+
else arr.push(num);
61+
});
62+
63+
return arr.length;
64+
}

โ€Žspiral-matrix/mmyeon.tsโ€Ž

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @link https://leetcode.com/problems/spiral-matrix/description/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - right -> bottom -> left -> top ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ฒฝ๊ณ„ ์—…๋ฐ์ดํŠธ ์ฒ˜๋ฆฌ
6+
*
7+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
8+
* - n์€ ํ–‰๋ ฌ์˜ ๋ชจ๋“  ์ˆซ์ž๋กœ, ๋ชจ๋“  ์ˆซ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ O(n)
9+
*
10+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
11+
* - ๋ชจ๋“  ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐฐ์—ด ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(n)
12+
*/
13+
14+
function spiralOrder(matrix: number[][]): number[] {
15+
const result: number[] = [];
16+
// ๊ฒฝ๊ณ„ ์„ค์ •
17+
let top = 0,
18+
bottom = matrix.length - 1,
19+
left = 0,
20+
right = matrix[0].length - 1;
21+
22+
const moveRight = () => {
23+
for (let i = left; i <= right; i++) {
24+
result.push(matrix[top][i]);
25+
}
26+
top++;
27+
};
28+
29+
const moveDown = () => {
30+
for (let i = top; i <= bottom; i++) {
31+
result.push(matrix[i][right]);
32+
}
33+
right--;
34+
};
35+
36+
const moveLeft = () => {
37+
for (let i = right; i >= left; i--) {
38+
result.push(matrix[bottom][i]);
39+
}
40+
bottom--;
41+
};
42+
43+
const moveUp = () => {
44+
for (let i = bottom; i >= top; i--) {
45+
result.push(matrix[i][left]);
46+
}
47+
left++;
48+
};
49+
50+
while (top <= bottom && left <= right) {
51+
moveRight();
52+
moveDown();
53+
54+
if (top <= bottom) moveLeft();
55+
if (left <= right) moveUp();
56+
}
57+
58+
return result;
59+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
*@link https://leetcode.com/problems/valid-parentheses/description/
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
5+
* - ๋‹ซํžŒ ๊ด„ํ˜ธ ๋‚˜์™”์„ ๋•Œ ์—ด๋ฆฐ ๊ด„ํ˜ธ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’๊ณผ ์ง์ด ๋งž๋Š”์ง€ ์ฐพ์•„์•ผ๋˜๋‹ˆ๊นŒ stack ์‚ฌ์šฉ
6+
*
7+
* ์‹œ๊ฐ„๋ณต์žก๋„ : O(n)
8+
* - ๋ฌธ์ž์—ด ์ˆœํšŒํ•˜๋ฉด์„œ ๊ด„ํ˜ธ์ฒดํฌํ•˜๋‹ˆ๊นŒ O(n)
9+
*
10+
* ๊ณต๊ฐ„๋ณต์žก๋„ : O(k)
11+
* - pairs ๊ฐ์ฒด ๊ณ ์ •๋œ ํฌ๊ธฐ๋กœ ์ €์žฅ O(1)
12+
* - k๋Š” ์—ด๋ฆฐ ๊ด„ํ˜ธ ๊ฐœ์ˆ˜, stack์— ์ „์ฒด ๋ฌธ์ž์—ด ์ค‘ k๋งŒํผ๋งŒ ๋‹ด๊ธฐ๋‹ˆ๊นŒ O(k)
13+
*/
14+
15+
function isValid(s: string): boolean {
16+
const pairs: Record<string, string> = {
17+
")": "(",
18+
"}": "{",
19+
"]": "[",
20+
};
21+
const stack: string[] = [];
22+
23+
for (const char of s) {
24+
// ๋‹ซ๋Š” ๊ด„ํ˜ธ ๋‚˜์˜จ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
25+
if (pairs[char]) {
26+
// ์ง์ด ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
27+
if (stack[stack.length - 1] !== pairs[char]) return false;
28+
29+
// ์ง์ด ๋งž๋Š” ๊ฒฝ์šฐ
30+
stack.pop();
31+
} else {
32+
// ์—ด๋ฆฐ ๊ด„ํ˜ธ๋Š” ๋ฐ”๋กœ stack์— ์ €์žฅ
33+
stack.push(char);
34+
}
35+
}
36+
37+
return stack.length === 0;
38+
}

0 commit comments

Comments
ย (0)