Skip to content

Commit 1726a64

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents a199b08 + 4fb656c commit 1726a64

File tree

10 files changed

+923
-83
lines changed

10 files changed

+923
-83
lines changed

best-time-to-buy-and-sell-stock/robinyoon-dev.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ var maxProfit = function (prices) {
2323
return maxProfit;
2424
};
2525

26-
2726
// -----아래는 이전에 작성한 답안입니다.
2827
// /**
2928
// * @param {number[]} prices
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/robinyoon-dev.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
6+
// 문제는 풀었으나 시간 복잡도 측면에서 효율이 너무 떨어지는 풀이 방법....
7+
var groupAnagrams = function (strs) {
8+
let outputArr = [];
9+
let countArr = [];
10+
11+
const A_ASCII = 'a'.charCodeAt(0);
12+
const Z_ASCII = 'z'.charCodeAt(0);
13+
14+
let charCounts = Z_ASCII - A_ASCII + 1;
15+
let charCountArr = new Array(charCounts).fill(0); //인덱스가 알파벳을 나타냄.
16+
17+
for (str of strs) {
18+
let strCountString = getStrCountString(str);
19+
20+
let hasSameCountIndex = countArr.findIndex((item) => item === strCountString);
21+
22+
if (hasSameCountIndex !== -1) {
23+
outputArr[hasSameCountIndex].push(str);
24+
} else {
25+
countArr.push(strCountString);
26+
27+
outputArr.push([str]);
28+
}
29+
}
30+
31+
return outputArr;
32+
33+
function getStrCountString(str) {
34+
let tempArr = [...charCountArr];
35+
36+
for (char of str) {
37+
let charAscii = char.charCodeAt(0);
38+
let charIndex = charAscii - A_ASCII;
39+
tempArr[charIndex] += 1;
40+
}
41+
return tempArr.join(',');
42+
}
43+
};
44+
45+

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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// 문제풀이 해설 보고 푼 문제입니다.
2+
3+
var TrieNode = function () {
4+
this.children = {};
5+
this.isEnd = false;
6+
}
7+
8+
var Trie = function () {
9+
this.root = new TrieNode();
10+
};
11+
12+
/**
13+
* @param {string} word
14+
* @return {void}
15+
*/
16+
Trie.prototype.insert = function (word) {
17+
let currentNode = this.root;
18+
for (let i = 0; i < word.length; i++) {
19+
let char = word[i];
20+
if (!currentNode.children[char]) {
21+
currentNode.children[char] = new TrieNode();
22+
23+
}
24+
currentNode = currentNode.children[char];
25+
}
26+
27+
currentNode.isEnd = true;
28+
};
29+
30+
/**
31+
* @param {string} word
32+
* @return {boolean}
33+
*/
34+
Trie.prototype.search = function (word) {
35+
let currentNode = this.root;
36+
for (let i = 0; i < word.length; i++) {
37+
let char = word[i];
38+
if (!currentNode.children[char]) {
39+
return false;
40+
}
41+
currentNode = currentNode.children[char];
42+
}
43+
return currentNode.isEnd;
44+
};
45+
46+
/**
47+
* @param {string} prefix
48+
* @return {boolean}
49+
*/
50+
Trie.prototype.startsWith = function (prefix) {
51+
let currentNode = this.root;
52+
for (let i = 0; i < prefix.length; i++) {
53+
let char = prefix[i];
54+
if (!currentNode.children[char]) {
55+
return false;
56+
}
57+
currentNode = currentNode.children[char];
58+
}
59+
return true;
60+
};
61+
62+
/**
63+
* Your Trie object will be instantiated and called as such:
64+
* var obj = new Trie()
65+
* obj.insert(word)
66+
* var param_2 = obj.search(word)
67+
* var param_3 = obj.startsWith(prefix)
68+
*/
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+
*/

0 commit comments

Comments
 (0)