Skip to content

Commit 1147199

Browse files
authored
Merge pull request #2495 from robinyoon-dev/main
[robinyoon-dev] WEEK 05 Solutions
2 parents ee612fa + 3679244 commit 1147199

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
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

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+
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+
*/

word-break/robinyoon-dev.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 문풀 해설 보고 푼 문제
2+
/**
3+
* @param {string} s
4+
* @param {string[]} wordDict
5+
* @return {boolean}
6+
*/
7+
var wordBreak = function (s, wordDict) {
8+
9+
const dp = new Array(s.length + 1).fill(false);
10+
dp[0] = true;
11+
12+
let maxLength = 0;
13+
for (let word of wordDict) {
14+
maxLength = Math.max(maxLength, word.length);
15+
}
16+
17+
for (let i = 1; i <= s.length; i++) {
18+
for (let j = Math.max(0, i - maxLength); j < i; j++) {
19+
if (dp[j] && wordDict.includes(s.substring(j, i))) {
20+
dp[i] = true;
21+
break;
22+
}
23+
}
24+
}
25+
26+
return dp[s.length];
27+
};

0 commit comments

Comments
 (0)