Skip to content

Commit dcbbef5

Browse files
authored
Merge pull request #880 from HiGeuni/main
[khyo] Week 5
2 parents 1e1fab7 + 2230c94 commit dcbbef5

File tree

3 files changed

+141
-0
lines changed

3 files changed

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

group-anagrams/higeuni.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*
5+
* complexity
6+
* time: O(n * m)
7+
* space: O(n)
8+
*
9+
* 풀이
10+
* 처음에는 각 알파벳에 숫자를 할당하여 합을 이용해서 풀이하는 방식으로 접근하려고 했으나,
11+
* 합의 경우의 수가 너무 많아서 중복되는 경우가 생겨서 다른 풀이 방식을 생각했다.
12+
* -> 이후 소수를 이용한 풀이를 생각했다. (소수의 곱을 이용하는 경우 중복되는 경우가 없다.)
13+
* 하지만 최악의 경우 소수의 곱이 너무 커져서 오버플로우가 발생한다.
14+
*
15+
* 그래서 정렬을 통해 각 문자열을 정렬하여 키로 사용하는 방식으로 접근했다.
16+
*/
17+
var groupAnagrams = function(strs) {
18+
const map = new Map();
19+
20+
for(const str of strs) {
21+
const sortedStr = str.split('').sort().join('');
22+
23+
if(!map.has(sortedStr)) {
24+
map.set(sortedStr, []);
25+
}
26+
map.get(sortedStr).push(str);
27+
}
28+
29+
return Array.from(map.values());
30+
};
31+
32+
/**
33+
* passed 되었으나, 최악의 경우에는 통과될 수 없지 않을까?
34+
*
35+
* @param {string[]} strs
36+
* @return {string[][]}
37+
*
38+
* complexity
39+
* time: O(n * m)
40+
* space: O(n)
41+
*/
42+
var groupAnagrams = function(strs) {
43+
const map = new Map();
44+
45+
const primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,
46+
43,47,53,59,61,67,71,73,79,83,89,97,101];
47+
48+
for(const str of strs) {
49+
let key = 1;
50+
for(const char of str) {
51+
key *= primes[char.charCodeAt(0) - 'a'.charCodeAt(0)];
52+
}
53+
54+
if(!map.has(key)) {
55+
map.set(key, []);
56+
}
57+
map.get(key).push(str);
58+
}
59+
60+
return Array.from(map.values());
61+
};
62+

implement-trie-prefix-tree/higeuni.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var Trie = function() {
2+
this.root = {};
3+
};
4+
5+
/**
6+
* @param {string} word
7+
* @return {void}
8+
*
9+
* complexity
10+
* time: O(n)
11+
* space: O(n)
12+
*/
13+
Trie.prototype.insert = function(word) {
14+
let cur = this.root;
15+
for (let x of word) {
16+
if (!cur[x]) cur[x] = {};
17+
cur = cur[x];
18+
}
19+
cur.end = true;
20+
};
21+
22+
/**
23+
* @param {string} word
24+
* @return {boolean}
25+
*
26+
* complexity
27+
* time: O(n)
28+
* space: O(1)
29+
*/
30+
Trie.prototype.search = function(word) {
31+
let cur = this.find(word);
32+
return cur !== null && cur.end === true;
33+
};
34+
35+
/**
36+
* @param {string} prefix
37+
* @return {boolean}
38+
*
39+
* complexity
40+
* time: O(n)
41+
* space: O(1)
42+
*/
43+
Trie.prototype.startsWith = function(prefix) {
44+
return this.find(prefix) !== null;
45+
};
46+
47+
/**
48+
* @param {string} str
49+
* @return {object}
50+
*
51+
* complexity
52+
* time: O(n)
53+
* space: O(1)
54+
*/
55+
Trie.prototype.find = function(str) {
56+
let cur = this.root;
57+
for (let x of str) {
58+
if (!cur[x]) return null;
59+
cur = cur[x];
60+
}
61+
return cur;
62+
};
63+

0 commit comments

Comments
 (0)