Skip to content

Commit e39c7ef

Browse files
authored
Merge pull request #1425 from soobing/week6
[soobing] WEEK 06 Solutions
2 parents 573d35b + b807172 commit e39c7ef

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

container-with-most-water/soobing.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
*
3+
* 문제 설명
4+
* 가장 많이 담을 수 있는 물의 용기 구하기
5+
* - height: 높이(n)를 담고 있는 배열 = y축 값
6+
* - index: x축 값
7+
*
8+
* 아이디어
9+
* 1. 브루트포스 방식 O(n^2)
10+
* - 모든 쌍을 비교하여 최대 물의 양 찾기
11+
*
12+
* 2. 투 포인터 방식 O(n)
13+
* - 왼쪽과 오른쪽 포인터를 이용하여 최대 물의 양 찾기
14+
* - 같은 높이의 두 라인이 있는 경우 한쪽만 움직여도 최적의 해를 찾는데는 문제 없음
15+
*/
16+
function maxArea(height: number[]): number {
17+
let left = 0;
18+
let right = height.length - 1;
19+
let result = 0;
20+
while (left < right) {
21+
const x = right - left;
22+
const y = Math.min(height[left], height[right]);
23+
result = Math.max(x * y, result);
24+
25+
if (height[left] < height[right]) {
26+
left++;
27+
} else {
28+
right--;
29+
}
30+
}
31+
32+
return result;
33+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
*
3+
* 문제 설명
4+
* - 문자열 추가/검색에 효율적인 데이터 구조 설계
5+
*
6+
* 아이디어
7+
* 1) 배열에 추가, 순차적으로 검색(.의 경우 정규식 사용)
8+
* - Time Limit Exceeded (TLE) 발생
9+
*
10+
* 2) Trie 구조로 저장, 검색은 dfs
11+
* - 문자열 검색어 최적화 = Trie
12+
*
13+
*/
14+
15+
class TrieNode {
16+
children: Map<string, TrieNode> = new Map();
17+
isEnd: boolean = false;
18+
}
19+
20+
class WordDictionary {
21+
root: TrieNode;
22+
23+
constructor() {
24+
this.root = new TrieNode();
25+
}
26+
27+
addWord(word: string): void {
28+
let node = this.root;
29+
for (let char of word) {
30+
if (!node.children.has(char)) {
31+
node.children.set(char, new TrieNode());
32+
}
33+
node = node.children.get(char)!;
34+
}
35+
node.isEnd = true;
36+
}
37+
38+
search(word: string): boolean {
39+
const dfs = (node: TrieNode, i: number) => {
40+
if (i === word.length) return node.isEnd;
41+
42+
const char = word[i];
43+
44+
if (char === ".") {
45+
for (let child of node.children.values()) {
46+
if (dfs(child, i + 1)) return true;
47+
}
48+
return false;
49+
} else {
50+
const next = node.children.get(char);
51+
return next ? dfs(next, i + 1) : false;
52+
}
53+
};
54+
55+
return dfs(this.root, 0);
56+
}
57+
}
58+
59+
/**
60+
* Your WordDictionary object will be instantiated and called as such:
61+
* var obj = new WordDictionary()
62+
* obj.addWord(word)
63+
* var param_2 = obj.search(word)
64+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
*
3+
* 문제 설명
4+
* - Longest Increasing Subsequence(LIS)
5+
* - 최장 증가 부분 수열
6+
*
7+
* 아이디어
8+
* 1) Brute Force
9+
* - 시간복잡도가 O(2^n)이라서 TLE(Time Limit Exceed) 발생
10+
* - 구현은 backtracking으로 해야함.
11+
*
12+
* 2) Dynamic Programming
13+
* - 시간복잡도: O(n^2)
14+
* - nums[i]의 이전 원소들 중 가장 긴 LIS에 1을 더해서 저장
15+
*
16+
* 3) Binary Search
17+
* - 시간 복잡도: (O(n log n))
18+
* - 다음 기회에 풀어봐야지..
19+
*
20+
*/
21+
function lengthOfLIS(nums: number[]): number {
22+
const dp = Array(nums.length).fill(1);
23+
24+
for (let i = 0; i < nums.length; i++) {
25+
for (let j = 0; j < i; j++) {
26+
if (nums[j] < nums[i]) {
27+
dp[i] = Math.max(dp[i], dp[j] + 1);
28+
}
29+
}
30+
}
31+
32+
return Math.max(...dp);
33+
}

spiral-matrix/soobing.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
*
3+
* 문제 설명
4+
* - 2차원 배열을 나선형으로 데이터 순회하여 1차원 배열로 담기
5+
* - 문제 풀이 타입의 알고리즘 문제 같음
6+
*
7+
* 아이디어
8+
* 1) 경계를 정하고 오른쪽, 아래, 왼쪽, 위로 순회한다.
9+
*
10+
*/
11+
function spiralOrder(matrix: number[][]): number[] {
12+
let left = 0;
13+
let top = 0;
14+
let right = matrix[0].length - 1;
15+
let bottom = matrix.length - 1;
16+
17+
const result: number[] = [];
18+
19+
while (left <= right && top <= bottom) {
20+
// 오른쪽
21+
for (let i = left; i <= right; i++) {
22+
result.push(matrix[top][i]);
23+
}
24+
top++;
25+
26+
// 아래
27+
for (let i = top; i <= bottom; i++) {
28+
result.push(matrix[i][right]);
29+
}
30+
right--;
31+
32+
// 왼쪽
33+
if (top <= bottom) {
34+
for (let i = right; i >= left; i--) {
35+
result.push(matrix[bottom][i]);
36+
}
37+
bottom--;
38+
}
39+
40+
// 위쪽
41+
if (left <= right) {
42+
for (let i = bottom; i >= top; i--) {
43+
result.push(matrix[i][left]);
44+
}
45+
left++;
46+
}
47+
}
48+
return result;
49+
}

valid-parentheses/soobing.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function isValid(s: string): boolean {
2+
const result: string[] = [];
3+
const open = ["(", "[", "{"];
4+
const close = [")", "]", "}"];
5+
for (let i = 0; i < s.length; i++) {
6+
if (open.includes(s[i])) {
7+
result.push(s[i]);
8+
} else {
9+
const index = close.findIndex((item) => item === s[i]);
10+
const popedItem = result.pop();
11+
if (popedItem !== open[index]) return false;
12+
}
13+
}
14+
return result.length === 0;
15+
}

0 commit comments

Comments
 (0)