Skip to content

Commit 291bcfd

Browse files
authored
Merge pull request #1395 from lhc0506/main
[lhc0506] WEEK 05 solutions
2 parents 0c509d0 + 5b98f10 commit 291bcfd

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let minPrice = Number.MAX_SAFE_INTEGER;
7+
let maxProfit = 0;
8+
9+
for (let i = 0; i < prices.length; i++) {
10+
minPrice = Math.min(minPrice, prices[i]);
11+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
12+
}
13+
14+
return maxProfit;
15+
};

encode-and-decode-strings/lhc0506.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* @param {string[]} strs
4+
* @returns {string}
5+
*/
6+
encode(strs) {
7+
return JSON.stringify(strs);
8+
}
9+
10+
/**
11+
* @param {string} str
12+
* @returns {string[]}
13+
*/
14+
decode(str) {
15+
return JSON.parse(str);
16+
}
17+
}

group-anagrams/lhc0506.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function(strs) {
6+
const A_ASCII = 'a'.charCodeAt();
7+
8+
const anagramMap = new Map();
9+
10+
for (const str of strs) {
11+
const counter = new Array(26).fill(0);
12+
13+
for (let i = 0; i < str.length; i++) {
14+
counter[str.charCodeAt(i) - A_ASCII]++;
15+
}
16+
17+
let key = '';
18+
for (let i = 0; i < 26; i++) {
19+
if (counter[i] > 0) {
20+
key += String.fromCharCode(i + A_ASCII) + counter[i];
21+
}
22+
}
23+
24+
if (!anagramMap.has(key)) {
25+
anagramMap.set(key, []);
26+
}
27+
anagramMap.get(key).push(str);
28+
}
29+
30+
return Array.from(anagramMap.values());
31+
};
32+
33+
// 시간 복잡도: O(n), 공간 복잡도: O(n)

implement-trie-prefix-tree/lhc0506.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
var Trie = function() {
3+
this.isEnd = false;
4+
};
5+
6+
/**
7+
* @param {string} word
8+
* @return {void}
9+
*/
10+
Trie.prototype.insert = function(word) {
11+
let currentTrie = this;
12+
word.split('').forEach(alphabet => {
13+
if (!currentTrie[alphabet]) {
14+
currentTrie[alphabet] = new Trie();
15+
}
16+
currentTrie = currentTrie[alphabet];
17+
});
18+
19+
currentTrie.isEnd = true;
20+
};
21+
22+
/**
23+
* @param {string} word
24+
* @return {boolean}
25+
*/
26+
Trie.prototype.search = function(word) {
27+
let currentTrie = this;
28+
29+
for (let alphabet of word) {
30+
if (!currentTrie[alphabet]) {
31+
return false;
32+
}
33+
currentTrie = currentTrie[alphabet];
34+
}
35+
36+
return currentTrie.isEnd;
37+
};
38+
39+
/**
40+
* @param {string} prefix
41+
* @return {boolean}
42+
*/
43+
Trie.prototype.startsWith = function(prefix) {
44+
let currentTrie = this;
45+
46+
for (let alphabet of prefix) {
47+
if (!currentTrie[alphabet]) {
48+
return false;
49+
}
50+
currentTrie = currentTrie[alphabet];
51+
}
52+
53+
return true;
54+
};
55+
56+
/**
57+
* Your Trie object will be instantiated and called as such:
58+
* var obj = new Trie()
59+
* obj.insert(word)
60+
* var param_2 = obj.search(word)
61+
* var param_3 = obj.startsWith(prefix)
62+
*/
63+
64+
65+
// insert 시간복잡도: O(n), search 시간복잡도: O(n), startsWith 시간복잡도: O(n)

word-break/lhc0506.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
var wordBreak = function(s, wordDict) {
7+
const wordDictSet = new Set(wordDict);
8+
const visited = new Set();
9+
10+
const indexStack = [0];
11+
while(indexStack.length > 0) {
12+
const startIndex = indexStack.pop();
13+
14+
if (visited.has(startIndex)) continue;
15+
visited.add(startIndex);
16+
17+
if (startIndex === s.length) return true;
18+
19+
for (let endIndex = startIndex + 1; endIndex <= s.length; endIndex++) {
20+
const word = s.substring(startIndex, endIndex);
21+
22+
if (wordDictSet.has(word)) {
23+
indexStack.push(endIndex);
24+
}
25+
}
26+
}
27+
28+
return false;
29+
};

0 commit comments

Comments
 (0)