Skip to content

Commit 4fb656c

Browse files
authored
Merge pull request #2510 from soobing/week5
[soobing] WEEK 05 Solutions
2 parents 1147199 + 9a45a0d commit 4fb656c

File tree

5 files changed

+80
-82
lines changed

5 files changed

+80
-82
lines changed
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
// 3rd tried
12
function maxProfit(prices: number[]): number {
2-
let minPrice = Infinity;
3-
let maxProfit = 0;
4-
for (let i = 0; i < prices.length; i++) {
5-
if (prices[i] < minPrice) {
6-
minPrice = prices[i];
7-
}
3+
let left = 0;
4+
let right = 1;
5+
let max = 0;
86

9-
if (prices[i] - minPrice > maxProfit) {
10-
maxProfit = prices[i] - minPrice;
11-
}
7+
while(left < right && right < prices.length) {
8+
if(prices[right] - prices[left] > 0) {
9+
max = Math.max(max, prices[right] - prices[left])
10+
} else {
11+
left = right;
12+
}
13+
right++;
1214
}
13-
return maxProfit;
14-
}
15+
return max;
16+
};
Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1+
// 3rd tried
12
class Solution {
23
/**
3-
* 문자열 배열을 하나의 문자열로 인코딩합니다.
4-
* @param strs - 문자열 배열
5-
* @returns 인코딩된 하나의 문자열
4+
* @param {string[]} strs
5+
* @returns {string}
66
*/
7-
encode(strs: string[]): string {
8-
return strs.map((str) => str.length + "#" + str).join("");
7+
encode(strs) {
8+
return strs.map((str) => `${str.length}#${str}`).join('');
99
}
1010

1111
/**
12-
* 인코딩된 문자열을 원래 문자열 배열로 디코딩합니다.
13-
* @param str - 인코딩된 문자열
14-
* @returns 디코딩된 문자열 배열
12+
* @param {string} str
13+
* @returns {string[]}
1514
*/
16-
decode(str: string): string[] {
17-
const result: string[] = [];
18-
19-
let i = 0;
20-
while (i < str.length) {
21-
let j = i;
22-
while (str[j] !== "#") {
23-
j++;
15+
decode(str) {
16+
const result: string[] = [];
17+
let i = 0;
18+
while(i < str.length) {
19+
const j = str.indexOf('#', i);
20+
const length = Number(str.slice(i, j));
21+
result.push(str.slice(j + 1, j + 1 + length));
22+
i = j + 1 + length;
2423
}
25-
26-
const length = parseInt(str.slice(i, j));
27-
const word = str.slice(j + 1, j + 1 + length);
28-
result.push(word);
29-
i = j + 1 + length;
30-
}
31-
32-
return result;
24+
return result;
3325
}
3426
}

group-anagrams/soobing.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
// idea: 배열에 담긴 모든 애들을 다 sorting하면서 sorting된 결과를 key로 바인딩하고 Record<string, string[]> 에 맞게 매핑하여 values들만 리턴하면 될것 같음
1+
// 3rd tried
22
function groupAnagrams(strs: string[]): string[][] {
3-
const map = new Map<string, string[]>();
3+
const map = new Map();
44

5-
for (let i = 0; i < strs.length; i++) {
6-
const key = strs[i].split("").sort().join("");
7-
const group = map.get(key);
8-
if (group) {
9-
group.push(strs[i]);
10-
} else {
11-
map.set(key, [strs[i]]);
12-
}
5+
for(const str of strs) {
6+
const key = str.split('').sort().join('');
7+
const value = map.get(key);
8+
map.set(key, value ? [...value, str] : [str])
139
}
14-
return [...map.values()];
15-
}
10+
return Array.from(map.values());
11+
};
Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,53 @@
1+
// 3rd tried
12
class TrieNode {
23
children: Map<string, TrieNode>;
34
isEnd: boolean;
4-
55
constructor() {
6-
this.children = new Map();
7-
this.isEnd = false;
6+
this.children = new Map();
7+
this.isEnd = false
88
}
99
}
10-
1110
class Trie {
1211
root: TrieNode;
1312

1413
constructor() {
15-
this.root = new TrieNode();
14+
this.root = new TrieNode();
1615
}
1716

1817
insert(word: string): void {
19-
let node = this.root;
20-
for (const char of word) {
21-
if (!node.children.has(char)) {
22-
node.children.set(char, new TrieNode());
18+
let node = this.root;
19+
for(const ch of word) {
20+
if(!node.children.has(ch)) {
21+
node.children.set(ch, new TrieNode());
22+
}
23+
node = node.children.get(ch)!
2324
}
24-
node = node.children.get(char)!;
25-
}
26-
node.isEnd = true;
25+
node.isEnd = true;
2726
}
2827

2928
search(word: string): boolean {
30-
const node = this._findNode(word);
31-
return node !== null && node.isEnd;
29+
let node = this.root;
30+
for(const ch of word) {
31+
if(!node.children.has(ch)) return false;
32+
node = node.children.get(ch)!;
33+
}
34+
return node.isEnd;
3235
}
3336

3437
startsWith(prefix: string): boolean {
35-
return this._findNode(prefix) !== null;
36-
}
37-
38-
private _findNode(word: string): TrieNode | null {
39-
let node = this.root;
40-
for (const char of word) {
41-
if (!node.children.has(char)) return null;
42-
node = node.children.get(char)!;
43-
}
44-
return node;
38+
let node = this.root;
39+
for(const ch of prefix) {
40+
if(!node.children.has(ch)) return false;
41+
node = node.children.get(ch)!;
42+
}
43+
return true;
4544
}
4645
}
46+
47+
/**
48+
* Your Trie object will be instantiated and called as such:
49+
* var obj = new Trie()
50+
* obj.insert(word)
51+
* var param_2 = obj.search(word)
52+
* var param_3 = obj.startsWith(prefix)
53+
*/

word-break/soobing.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
// 3rd tried
12
function wordBreak(s: string, wordDict: string[]): boolean {
23
const dp = new Array(s.length + 1).fill(false);
3-
dp[s.length] = true;
4+
dp[0] = true;
45

5-
for (let i = s.length - 1; i >= 0; i--) {
6-
for (const word of wordDict) {
7-
if (i + word.length <= s.length && s.slice(i, i + word.length) === word) {
8-
dp[i] = dp[i + word.length];
9-
}
10-
11-
if (dp[i]) break;
6+
for (let i = 1; i <= s.length; i++) {
7+
for (let j = 0; j < i; j++) {
8+
if (dp[j] && wordDict.includes(s.slice(j, i))) {
9+
dp[i] = true;
10+
break;
11+
}
12+
}
1213
}
13-
}
14-
return dp[0];
15-
}
14+
15+
return dp[s.length];
16+
};

0 commit comments

Comments
 (0)