Skip to content

Commit 5bef82a

Browse files
authored
Merge pull request #2207 from Blossssom/main
2 parents 6426e9e + b7f13bb commit 5bef82a

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param height - 정수 배열
3+
* @returns - 용기에 담을 수 있는 최대 물의 양
4+
* @description
5+
* - 양 끝에서 2포인터를 사용해 크기를 체크해 나아감
6+
* - 결국 거리가 짧아지기 때문에 양쪽 높이를 비교하며 높이가 낮은 포인터를 움직이며 계산
7+
* - 시간 복잡도 O(n)
8+
*/
9+
10+
function maxArea(height: number[]): number {
11+
let left = 0;
12+
let right = height.length - 1;
13+
let water = 0;
14+
15+
while (left < right) {
16+
const calcWater = (right - left) * Math.min(height[left], height[right]);
17+
if (water < calcWater) {
18+
water = calcWater;
19+
}
20+
21+
if (height[left] < height[right]) {
22+
left++;
23+
} else {
24+
right--;
25+
}
26+
}
27+
28+
return water;
29+
}
30+
31+
const height = [1, 3, 2, 5, 25, 24, 5];
32+
const result = maxArea(height);
33+
34+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @class - Dictionary 구현
3+
* @description
4+
* - .의 경우 모든 문자와 일치
5+
* - .일 경우 결국 모든 children 조회필요 => dfs
6+
*/
7+
8+
class WordNode {
9+
children: { [key: string]: WordNode };
10+
endOfWord: boolean;
11+
constructor() {
12+
this.children = {};
13+
this.endOfWord = false;
14+
}
15+
}
16+
17+
class WordDictionary {
18+
private rootNode: WordNode;
19+
constructor() {
20+
this.rootNode = new WordNode();
21+
}
22+
23+
addWord(word: string): void {
24+
let nodeInstance = this.rootNode;
25+
26+
for (const str of word) {
27+
if (!nodeInstance.children[str]) {
28+
nodeInstance.children[str] = new WordNode();
29+
}
30+
nodeInstance = nodeInstance.children[str];
31+
}
32+
33+
nodeInstance.endOfWord = true;
34+
}
35+
36+
search(word: string): boolean {
37+
return this.searchDfs(this.rootNode, word, 0);
38+
}
39+
40+
private searchDfs(currentNode: WordNode, word: string, idx: number): boolean {
41+
if (idx === word.length) {
42+
return currentNode.endOfWord;
43+
}
44+
45+
const char = word[idx];
46+
if (char === ".") {
47+
for (const key in currentNode.children) {
48+
const childSearch = this.searchDfs(
49+
currentNode.children[key],
50+
word,
51+
idx + 1
52+
);
53+
if (childSearch) {
54+
return true;
55+
}
56+
}
57+
58+
return false;
59+
} else {
60+
if (!currentNode.children[char]) {
61+
return false;
62+
}
63+
64+
return this.searchDfs(currentNode.children[char], word, idx + 1);
65+
}
66+
}
67+
}
68+
69+
const wordDict = new WordDictionary();
70+
71+
wordDict.addWord("at");
72+
wordDict.addWord("and");
73+
wordDict.addWord("an");
74+
wordDict.addWord("add");
75+
76+
wordDict.search("a"); // false
77+
wordDict.search(".at"); // false
78+
79+
wordDict.addWord("bat");
80+
81+
wordDict.search(".at"); // true
82+
wordDict.search("an."); // true
83+
wordDict.search("a.d."); // false
84+
wordDict.search("b."); // false
85+
wordDict.search("a.d"); // true
86+
wordDict.search("."); // false
87+
88+
89+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param nums - 정수 배열
3+
* @returns - 엄격하게 증가하는 요소의 길이 반환
4+
*/
5+
function lengthOfLIS(nums: number[]): number {
6+
const arr: number[] = Array.from({ length: nums.length }, () => 1);
7+
let maxLen = 1;
8+
for (let i = 1; i < nums.length; i++) {
9+
for (let j = 0; j < i; j++) {
10+
if (nums[i] > nums[j]) {
11+
arr[i] = Math.max(arr[i], arr[j] + 1);
12+
}
13+
}
14+
maxLen = Math.max(maxLen, arr[i]);
15+
}
16+
17+
return maxLen;
18+
}
19+
20+
const nums = [0, 1, 0, 3, 2, 3];
21+
lengthOfLIS(nums);
22+
23+

valid-parentheses/Blossssom.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param s - 괄호 문자열
3+
* @returns - 올바르게 열고 닫혔는지 반환
4+
*/
5+
// function isValid(s: string): boolean {
6+
// const parentheses: Record<string, boolean> = {
7+
// "()": true,
8+
// "{}": true,
9+
// "[]": true,
10+
// };
11+
// const stack: string[] = [];
12+
// for (const st of s) {
13+
// if (
14+
// !stack.length ||
15+
// parentheses[`${stack[stack.length - 1]}${st}`] === undefined
16+
// ) {
17+
// stack.push(st);
18+
// } else {
19+
// stack.pop();
20+
// }
21+
// }
22+
// return stack.length ? false : true;
23+
// }
24+
25+
function isValid(s: string): boolean {
26+
const parentheses: Record<string, string> = {
27+
"(": ")",
28+
"{": "}",
29+
"[": "]",
30+
};
31+
32+
const stack: string[] = [];
33+
for (const char of s) {
34+
if (parentheses[char]) {
35+
stack.push(char);
36+
} else {
37+
const last = stack.pop();
38+
if (last === undefined || parentheses[last] !== char) {
39+
return false;
40+
}
41+
}
42+
}
43+
44+
return !stack.length;
45+
}
46+
47+

0 commit comments

Comments
 (0)