Skip to content

Commit ebda7ae

Browse files
authored
Merge pull request #845 from gwbaik9717/main
[ganu] Week 5
2 parents e9bff47 + 2d16acd commit ebda7ae

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[]} prices
6+
* @return {number}
7+
*/
8+
var maxProfit = function (prices) {
9+
let answer = 0;
10+
let minValue = Number.MAX_SAFE_INTEGER;
11+
12+
for (const price of prices) {
13+
minValue = Math.min(minValue, price);
14+
answer = Math.max(answer, price - minValue);
15+
}
16+
17+
return answer;
18+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// n: len(str)
2+
// Time complexity: O(n)
3+
// Space complexity: O(1)
4+
const encode = function (arr) {
5+
let answer = "";
6+
7+
for (const word of arr) {
8+
answer += `${word.length}${SEPERATOR}`;
9+
}
10+
11+
return answer;
12+
};
13+
14+
// n: len(str)
15+
// Time complexity: O(n)
16+
// Space complexity: O(n)
17+
const decode = function (str) {
18+
const SEPERATOR = "|";
19+
const words = [];
20+
21+
let i = 0;
22+
let wordLength = "";
23+
24+
while (i < str.length) {
25+
if (str[i] === SEPERATOR) {
26+
words.push(str.slice(i + 1, i + 1 + Number(wordLength)));
27+
i += Number(wordLength) + 1;
28+
wordLength = "";
29+
continue;
30+
}
31+
32+
wordLength += str[i];
33+
i += 1;
34+
}
35+
36+
return words;
37+
};

group-anagrams/gwbaik9717.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// n: length of strs, m: length of strs[i]
2+
// Time complexity: O(nm)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {string[]} strs
7+
* @return {string[][]}
8+
*/
9+
var groupAnagrams = function (strs) {
10+
const answer = [];
11+
const anagramDict = new Map();
12+
13+
const getKey = (str) => {
14+
const minCharCode = "a".charCodeAt();
15+
const maxCharCode = "z".charCodeAt();
16+
17+
const counter = Array.from(
18+
{ length: maxCharCode - minCharCode + 1 },
19+
() => 0
20+
);
21+
22+
for (const chr of str) {
23+
const index = chr.charCodeAt() - minCharCode;
24+
counter[index]++;
25+
}
26+
27+
return counter.join("#");
28+
};
29+
30+
for (let i = 0; i < strs.length; i++) {
31+
const str = strs[i];
32+
const key = getKey(str);
33+
34+
if (!anagramDict.has(key)) {
35+
anagramDict.set(key, []);
36+
}
37+
38+
anagramDict.get(key).push(str);
39+
}
40+
41+
for (const [_, value] of anagramDict) {
42+
answer.push(value);
43+
}
44+
45+
return answer;
46+
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var Node = function () {
2+
this.children = new Map();
3+
this.isEnd = false;
4+
};
5+
6+
var Trie = function () {
7+
this.head = new Node();
8+
};
9+
10+
/**
11+
* @param {string} str
12+
* @return {TrieNode | null}
13+
*/
14+
Trie.prototype._traverse = function (str) {
15+
let current = this.head;
16+
17+
for (const chr of str) {
18+
if (!current.children.has(chr)) {
19+
return null;
20+
}
21+
current = current.children.get(chr);
22+
}
23+
24+
return current;
25+
};
26+
27+
/**
28+
* @param {string} word
29+
* @return {void}
30+
*/
31+
Trie.prototype.insert = function (word) {
32+
let current = this.head;
33+
34+
for (const chr of word) {
35+
if (!current.children.has(chr)) {
36+
current.children.set(chr, new Node());
37+
}
38+
39+
current = current.children.get(chr);
40+
}
41+
42+
current.isEnd = true;
43+
};
44+
45+
/**
46+
* @param {string} word
47+
* @return {boolean}
48+
*/
49+
Trie.prototype.search = function (word) {
50+
const node = this._traverse(word);
51+
52+
if (!node) {
53+
return false;
54+
}
55+
56+
return node.isEnd;
57+
};
58+
59+
/**
60+
* @param {string} prefix
61+
* @return {boolean}
62+
*/
63+
Trie.prototype.startsWith = function (prefix) {
64+
const node = this._traverse(prefix);
65+
66+
if (!node) {
67+
return false;
68+
}
69+
70+
return true;
71+
};
72+
73+
/**
74+
* Your Trie object will be instantiated and called as such:
75+
* var obj = new Trie()
76+
* obj.insert(word)
77+
* var param_2 = obj.search(word)
78+
* var param_3 = obj.startsWith(prefix)
79+
*/

word-break/gwbaik9717.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// n: len(s), m: len(wordDict)
2+
// Time complexity: O(n^2*m)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {string} s
7+
* @param {string[]} wordDict
8+
* @return {boolean}
9+
*/
10+
var wordBreak = function (s, wordDict) {
11+
const dp = Array.from({ length: s.length + 1 }, () => false);
12+
dp[0] = true;
13+
14+
for (let i = 1; i <= s.length; i++) {
15+
for (const word of wordDict) {
16+
const sliced = s.slice(i - word.length, i);
17+
18+
if (word === sliced && !dp[i]) {
19+
dp[i] = dp[i - word.length];
20+
}
21+
}
22+
}
23+
24+
return dp.at(-1);
25+
};

0 commit comments

Comments
 (0)