Skip to content

Commit cde9f5c

Browse files
committed
week5
1 parent 606b9ae commit cde9f5c

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
일단 가장 단순하게 생각했을 때,
3+
n^2 의 시간 복잡도를 가지면 모든 경우의 수를 확인가능하다.
4+
5+
function maxProfit(prices: number[]): number {
6+
let maximum = 0
7+
for(let i=0; i<prices.length-1; i++) {
8+
for(let j=i; j<prices.length; j++) {
9+
if(prices[j] - prices[i] > maximum) maximum = prices[j] - prices[i]
10+
}
11+
}
12+
return maximum
13+
};
14+
*/
15+
16+
/**
17+
하지만 시간 초과가 발생하니, 다른 방법을 써보자.
18+
이익을 구하기 위해서는 가장 낮은 지점과 가장 큰 지점에 대한 탐색이 필요하다.
19+
가장 낮은 지점은 큰 지점보다 앞에 위치해야 한다.
20+
21+
만약 투 포인터로 양 끝에서 오면서 왼쪽은 가장 낮은 것, 오른쪽은 가장 높은 것을 확인해서
22+
두 포인터가 만날 때 차분을 구한다면?
23+
24+
function maxProfit(prices: number[]): number {
25+
let left = prices[0];
26+
let right = prices[prices.length-1];
27+
for (let i=0; i<prices.length; i++) {
28+
if (i > (prices.length - 1 - i)) {
29+
break
30+
}
31+
32+
if (prices[i] < left) {
33+
left = prices[i]
34+
}
35+
36+
if (prices[prices.length - 1 - i] > right) {
37+
right = prices[prices.length - 1 - i]
38+
}
39+
}
40+
41+
return left > right ? 0 : right - left
42+
};
43+
*/
44+
45+
/**
46+
[3,2,6,5,0,3] 와 같은 경우에 일반화 되지 못한다.
47+
정답을 기준으로, 큰 값 이후에는 그보다 큰 값이 없어야 하고,
48+
작은 값 이전에는 그보다 작은 값이 없어야 한다.
49+
50+
그렇다면 포인터를 이 기준에 맞게 급격히 옮겨볼까?
51+
...
52+
힌트를 보고 왔다. 단순하게 최저값을 항상 갱신해주면 되는구나.
53+
*/
54+
function maxProfit(prices: number[]): number {
55+
let min = 10000;
56+
let max = 0;
57+
for (let i = 0; i < prices.length; i++) {
58+
if (prices[i] < min) {
59+
min = prices[i];
60+
continue;
61+
}
62+
63+
if (prices[i] - min > max) {
64+
max = prices[i] - min;
65+
}
66+
}
67+
return max;
68+
}

group-anagrams/Baekwangho.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
3글자씩으로 구성된 각 문자열 배열을 순회하며, 각 문자열을 알파벳 순서로 정렬한 순서를 기준으로
3+
동일하다면 원래의 문자열을 key 로 하는 해시에 포함시켜보자
4+
5+
시간 복잡도: O(n log n) 공간 복잡도: O(n) 정도 나오지 않을까?
6+
*/
7+
function groupAnagrams(strs: string[]): string[][] {
8+
const anaMap = new Map<string, string[]>();
9+
10+
for (let i = 0; i < strs.length; i++) {
11+
const hashKey = strs[i].split("").sort().join("");
12+
13+
const arr = anaMap.get(hashKey);
14+
if (!arr) {
15+
anaMap.set(hashKey, [strs[i]]);
16+
} else {
17+
anaMap.set(hashKey, [...arr, strs[i]]);
18+
}
19+
}
20+
21+
return [...anaMap.values()];
22+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
test case 를 분석해본 결과, 결국 문자열에 대한 트리를 구성해야 하는 것으로 보임
3+
트리는 노드로 구성할 수 있을 듯 하지만, 구현하지 않고 hash 의 연속을 만들어보는 것으로 하자.
4+
5+
오래걸렸지만 한번에 성공! 시간 복잡도: O(n), 공간 복잡도: O(1) 정도인 듯
6+
*/
7+
class Trie {
8+
constructor(private letterHash: any = {}) {}
9+
10+
insert(word: string): void {
11+
let currentHash = this.letterHash;
12+
13+
for (let i = 0; i < word.length; i++) {
14+
const existHash = currentHash[word[i]];
15+
if (existHash) {
16+
currentHash = existHash;
17+
} else {
18+
currentHash[word[i]] = {};
19+
currentHash = currentHash[word[i]];
20+
}
21+
}
22+
23+
currentHash["count"] = currentHash["count"] ? currentHash["count"] + 1 : 1;
24+
}
25+
26+
search(word: string): boolean {
27+
let currentHash = this.letterHash;
28+
29+
for (let i = 0; i < word.length; i++) {
30+
const existHash = currentHash[word[i]];
31+
// console.log(`search ${word}`, JSON.stringify(existHash), word[i])
32+
if (existHash) {
33+
currentHash = existHash;
34+
} else {
35+
return false;
36+
}
37+
}
38+
39+
return !!currentHash["count"];
40+
}
41+
42+
startsWith(prefix: string): boolean {
43+
let currentHash = this.letterHash;
44+
45+
for (let i = 0; i < prefix.length; i++) {
46+
const existHash = currentHash[prefix[i]];
47+
// console.log(`startsWith ${prefix}`, JSON.stringify(existHash), prefix[i])
48+
if (existHash) {
49+
currentHash = existHash;
50+
} else {
51+
return false;
52+
}
53+
}
54+
55+
return true;
56+
}
57+
}
58+
59+
/**
60+
* Your Trie object will be instantiated and called as such:
61+
* var obj = new Trie()
62+
* obj.insert(word)
63+
* var param_2 = obj.search(word)
64+
* var param_3 = obj.startsWith(prefix)
65+
*/

0 commit comments

Comments
 (0)