Skip to content

Commit 0c509d0

Browse files
authored
Merge pull request #1407 from krokerdile/main
[krokerdile] Week 05 Solution
2 parents 71a7e6d + a92eb7e commit 0c509d0

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var maxProfit = function(prices) {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (let price of prices) {
6+
if (price < minPrice) {
7+
minPrice = price; // ๋” ์‹ผ ๊ฐ€๊ฒฉ์ด ๋‚˜ํƒ€๋‚˜๋ฉด ๊ฐฑ์‹ 
8+
} else {
9+
maxProfit = Math.max(maxProfit, price - minPrice); // ์ด์ต ๊ฐฑ์‹ 
10+
}
11+
}
12+
13+
return maxProfit;
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
/**
3+
* @param {string[]} strs
4+
* @returns {string}
5+
*/
6+
encode(strs) {
7+
let result = "";
8+
9+
for (const str of strs) {
10+
result += `${str.length}#${str}`;
11+
}
12+
13+
return result;
14+
}
15+
16+
/**
17+
* @param {string} str
18+
* @returns {string[]}
19+
*/
20+
decode(s) {
21+
let result = [];
22+
let i = 0;
23+
// 5#hello5#world
24+
while (i < s.length) {
25+
const pos = s.indexOf("#", i);
26+
const len = parseInt(s.slice(i, pos));// 5
27+
i = pos + 1;
28+
const str = s.slice(i, i + len);
29+
result.push(str);
30+
i += len;
31+
}
32+
return result;
33+
}
34+
}
35+

โ€Žgroup-anagrams/krokerdile.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function(strs) {
6+
const dict = new Map();
7+
8+
strs.forEach(str => {
9+
const sorted = str.split('').sort().join('');
10+
if (!dict.has(sorted)) {
11+
dict.set(sorted, [str]);
12+
} else {
13+
dict.get(sorted).push(str);
14+
}
15+
});
16+
17+
// value ๊ธธ์ด ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
18+
const result = [...dict.entries()]
19+
.sort((a, b) => b[1].length - a[1].length)
20+
.map(([_, group]) => group); // value (์ฆ‰, ์•„๋‚˜๊ทธ๋žจ ๊ทธ๋ฃน)๋งŒ ๊บผ๋ƒ„
21+
22+
return result;
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var Trie = function() {
2+
this.root = {}; // ๋ฃจํŠธ๋Š” ๋นˆ ๊ฐ์ฒด๋กœ ์‹œ์ž‘
3+
};
4+
5+
/**
6+
* @param {string} word
7+
* @return {void}
8+
*/
9+
Trie.prototype.insert = function(word) {
10+
let node = this.root;
11+
for (let ch of word) {
12+
if (!node[ch]) node[ch] = {};
13+
node = node[ch];
14+
}
15+
node.isEnd = true; // ๋‹จ์–ด์˜ ๋์„ ํ‘œ์‹œ
16+
};
17+
18+
/**
19+
* @param {string} word
20+
* @return {boolean}
21+
*/
22+
Trie.prototype.search = function(word) {
23+
let node = this.root;
24+
for (let ch of word) {
25+
if (!node[ch]) return false;
26+
node = node[ch];
27+
}
28+
return node.isEnd === true; // ๋‹จ์–ด์˜ ๋์ด์–ด์•ผ๋งŒ true
29+
};
30+
31+
/**
32+
* @param {string} prefix
33+
* @return {boolean}
34+
*/
35+
Trie.prototype.startsWith = function(prefix) {
36+
let node = this.root;
37+
for (let ch of prefix) {
38+
if (!node[ch]) return false;
39+
node = node[ch];
40+
}
41+
return true; // ์ ‘๋‘์‚ฌ๋งŒ ๋งค์นญ๋˜๋ฉด true
42+
};

โ€Žword-break/krokerdile.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
var wordBreak = function(s, wordDict) {
7+
const wordSet = new Set(wordDict); // ๋น ๋ฅธ ๊ฒ€์ƒ‰์„ ์œ„ํ•œ Set
8+
const dp = Array(s.length + 1).fill(false);
9+
dp[0] = true; // ๋นˆ ๋ฌธ์ž์—ด์€ ํ•ญ์ƒ ๊ฐ€๋Šฅ
10+
11+
for (let i = 1; i <= s.length; i++) {
12+
for (let j = 0; j < i; j++) {
13+
const word = s.slice(j, i);
14+
if (dp[j] && wordSet.has(word)) {
15+
dp[i] = true;
16+
break; // ๋” ์ด์ƒ ๋ณผ ํ•„์š” ์—†์Œ
17+
}
18+
}
19+
}
20+
21+
return dp[s.length];
22+
};

0 commit comments

Comments
ย (0)