Skip to content

Commit 561b930

Browse files
Merge pull request #1852 from grapefruitgreentealoe/week5
[Grapefruitgreentealoe] Week 5 Solutions
2 parents 35344fc + 12ecc5e commit 561b930

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var maxProfit = function (prices) {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (let price of prices) {
6+
// ๊ฐ€์žฅ ์‹ผ ๊ฐ€๊ฒฉ์„ ๊ฐฑ์‹ 
7+
if (price < minPrice) {
8+
minPrice = price;
9+
} else {
10+
// ์ตœ๋Œ€ ์ด์ต์„ ๊ฐฑ์‹ 
11+
maxProfit = Math.max(maxProfit, price - minPrice);
12+
}
13+
}
14+
15+
return maxProfit;
16+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ๋„คํŠธ์›Œํฌ ์ „์†ก์„ ์œ„ํ•ด์„œ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ํ•ฉ์น˜๊ณ 
2+
// ๋‹ค์‹œ ๋ถ„๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
3+
4+
/**
5+
ํ’€์ด ๊ณผ์ •:
6+
๊ตฌ๋ถ„์ž๋ฅผ "a,b,c"์™€ ๊ฐ™์ด ,๋กœ joinํ•˜๋ฉด, ์›๋ž˜ ๋ฌธ์ž์—ด์— ,๊ฐ€ ํฌํ•จ๋  ๋•Œ ๊นจ์ง„๋‹ค.
7+
๋”ฐ๋ผ์„œ ๊ธธ์ด ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ํŠน์ˆ˜ escape ๋ฌธ์ž๋ฅผ ์จ์•ผํ•œ๋‹ค.
8+
9+
10+
*/
11+
/**
12+
* Encodes a list of strings to a single string.
13+
* @param {string[]} strs
14+
* @return {string}
15+
*/
16+
function encode(strs) {
17+
// ๊ฐ ๋ฌธ์ž์—ด ์•ž์— "๊ธธ์ด#"๋ฅผ ๋ถ™์ž„
18+
// ์˜ˆ: ["abc", "de"] -> "3#abc2#de"
19+
return strs.map((s) => `${s.length}#${s}`).join("");
20+
}
21+
22+
/**
23+
* Decodes a single string to a list of strings.
24+
* @param {string} s
25+
* @return {string[]}
26+
*/
27+
function decode(s) {
28+
let res = [];
29+
let i = 0;
30+
31+
while (i < s.length) {
32+
// 1. ๊ธธ์ด ์ฝ๊ธฐ
33+
let j = i;
34+
while (s[j] !== "#") j++;
35+
let length = parseInt(s.slice(i, j), 10);
36+
37+
// 2. ๊ธธ์ด์— ๋งž์ถฐ ๋ฌธ์ž์—ด ์ถ”์ถœ
38+
let str = s.slice(j + 1, j + 1 + length);
39+
res.push(str);
40+
41+
// 3. ์ธ๋ฑ์Šค ์ด๋™
42+
i = j + 1 + length;
43+
}
44+
45+
return res;
46+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function(strs) {
6+
const strsMap = new Map();
7+
for(let str of strs){
8+
const strArr = str.split('').sort()
9+
const keyName = strArr.join('-')
10+
strsMap.set(keyName,[...(strsMap.get(keyName)
11+
||[]),str])
12+
}
13+
return Array.from(strsMap.values())
14+
};
15+
16+
//์‹œ๊ฐ„๋ณต์žก๋„ : O(n * klogk)
17+
// ๊ณต๊ฐ„๋ณต์žก๋„ :O(n ยท k)
18+
19+
//2.๋ฌธ์ž ๋นˆ๋„์ˆ˜ ๊ธฐ๋ฐ˜ ํ‚ค ๋งŒ๋“ค๊ธฐ
20+
// ์•ŒํŒŒ๋ฒณ์ด 26๋ฟ์ด๋ฏ€๋กœ ๊ฐ ๋‹จ์–ด๋งˆ๋‹ค ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์„œ ์นด์šดํŠธ๋ฐฐ์—ด ๋งŒ๋“ค์–ด ์ด๊ฑธ ํ‚ค๋กœ ์‚ฌ์šฉ
21+
var groupAnagrams = function(strs) {
22+
const map = new Map();
23+
24+
for (let str of strs) {
25+
const count = new Array(26).fill(0);
26+
for (let char of str) {
27+
count[char.charCodeAt(0) - 97]++;
28+
}
29+
const key = count.join('#'); // ๊ตฌ๋ถ„์ž ์—†์œผ๋ฉด ["1","11"]๊ณผ ["11","1"] ๊ฐ™์€ ํ‚ค๋กœ ์˜คํ•ด ๊ฐ€๋Šฅ
30+
map.set(key, [...(map.get(key) || []), str]);
31+
}
32+
33+
return Array.from(map.values());
34+
};
35+
36+
//์‹œ๊ฐ„๋ณต์žก๋„ : O(n * k). ์ •๋ ฌ์ด ์—†์œผ๋ฏ€๋กœ
37+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n ยท k)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function TrieNode() {
2+
this.children = {};
3+
this.isEnd = false;
4+
}
5+
6+
function Trie() {
7+
this.root = new TrieNode();
8+
}
9+
10+
Trie.prototype.insert = function (word) {
11+
let node = this.root;
12+
for (let char of word) {
13+
if (!node.children[char]) {
14+
node.children[char] = new TrieNode();
15+
}
16+
node = node.children[char];
17+
}
18+
node.isEnd = true;
19+
};
20+
21+
Trie.prototype.search = function (word) {
22+
let node = this._traverse(word);
23+
return node !== null && node.isEnd === true;
24+
};
25+
26+
Trie.prototype.startsWith = function (prefix) {
27+
return this._traverse(prefix) !== null;
28+
};
29+
30+
Trie.prototype._traverse = function (str) {
31+
let node = this.root;
32+
for (let char of str) {
33+
if (!node.children[char]) return null;
34+
node = node.children[char];
35+
}
36+
return node;
37+
};
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
//1. ๋ฐฑํŠธ๋ ˆํ‚น
3+
4+
/**
5+
* @param {string} s
6+
* @param {string[]} wordDict
7+
* @return {boolean}
8+
*/
9+
10+
function findIndexAfter(arr, startIndex, callback) {
11+
for (let i = startIndex + 1; i < arr.length; i++) {
12+
if (callback(arr[i], i, arr)) {
13+
return i;
14+
}
15+
}
16+
return -1; // ๋ชป ์ฐพ์œผ๋ฉด -1
17+
}
18+
19+
var wordBreak = function(s, wordDict) {
20+
let current = '';
21+
const dp = new Array(s.length).fill('');
22+
for(let i=0;i<s.length;i++){
23+
const isSegment = wordDict.find((e) => e == current+s[i] )
24+
if(isSegment) {
25+
dp[i] = current + s[i]
26+
current = ''
27+
}else{
28+
current += s[i]
29+
}
30+
31+
if(current.length == s.length && i == s.length-1){
32+
return dp[i] ? true:false
33+
}
34+
35+
// ๋ฌธ์ž์—ด์ด ์ €์žฅ์ด ๋˜์–ด์žˆ๋Š” ์œ„์น˜์— ๋‹ค์‹œ ๋Œ์•„๊ฐ„๋‹ค.
36+
// ๊ทธ ์œ„์น˜์— ๋นˆ ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ค€ ํ›„ ๋‹ค์Œ index๋กœ ๋Œ์•„๊ฐ€๊ฒŒ๋” ํ•œ๋‹ค.
37+
if(i == s.length-1 && dp[s.length-1] == ''){
38+
i = dp.findLastIndex(e => e!=='')
39+
current = dp[i]
40+
dp[i] = '';
41+
}
42+
}
43+
return dp[s.length-1] ? true:false
44+
45+
};
46+
47+
48+
// ๊ฐ€์žฅ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด, ๋˜ ๊ฐ™์€์ผ์„ ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, ๊ฐ€์žฅ ํฐ ๊ฐ’์œผ๋กœ ๋Œ์•„๊ฐ€์•ผํ•˜๋Š”๊ฒŒ ๋งž๋‹ค.
49+
// ๊ทธ๋Ÿฐ๋ฐ ๊ทธ๋งŒํผ, ๋” ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
50+
51+
/*์‹œ๊ฐ„๋ณต์žก๋„: O(nยฒ * m) (n: ๋ฌธ์ž์—ด ๊ธธ์ด, m: wordDict ๊ธธ์ด)
52+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
53+
*/
54+
55+
//2. ์‹คํŒจ๋‚œ ์ง€์ ์„ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ•˜๊ธฐ : ์‹œ๊ฐ„๋ฆฌ๋ฐ‹์ด ๋‚œ ์ด์œ ๋Š”, ์ด์ „์— ์‹คํŒจ๋‚œ ์ง€์ ์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ๋Œ์•„๊ฐ„๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, word์˜ ์‹œ์ž‘๊ธฐ์ ์œผ๋กœ ์žก์œผ๋ฉด ๋ฌด์กฐ๊ฑด ์‹คํŒจ๊ฐ€ ๋‚˜๋Š” index๋ฅผ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ•œ๋‹ค.
56+
57+
/**
58+
* @param {string} s
59+
* @param {string[]} wordDict
60+
* @return {boolean}
61+
*/
62+
var wordBreak = function(s, wordDict) {
63+
const wordSet = new Set(wordDict); //์ค‘๋ณต์ด ๋˜์ง€ ์•Š๊ฒŒ ๋งŒ๋“ ๋‹ค.
64+
const failed = new Set(); //์‹คํŒจํ•œ ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ๋กํ•œ๋‹ค.
65+
66+
function dfs(start){
67+
if(start === s.length) return true; // ์„ฑ๊ณต
68+
if(failed.has(start)) return false; //์—ฌ๊ธธ ๊ธฐ์ ์œผ๋กœ ํ•˜๋ฉด ์‹คํŒจํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ๋ก.
69+
70+
for(let end = start+1; end<=s.length; end++){
71+
const word = s.slice(start,end);
72+
if(wordSet.has(word) && dfs(end)){ // ์—ฌ๊ธฐ์„œ ์ž˜๋ž์„ ๋•Œ ๊ฐ€๋Šฅํ•œ์ง€.
73+
return true;
74+
}
75+
}
76+
failed.add(start);
77+
return false;
78+
}
79+
return dfs(0);
80+
};
81+
82+
/*
83+
์‹œ๊ฐ„๋ณต์žก๋„: O(n^2) + ๋ฉ”๋ชจ์ด์ œ์ด์…˜์œผ๋กœ ์ตœ์ ํ™”
84+
๊ณต๊ฐ„๋ณต์žก๋„ : O(n) : ์žฌ๊ท€์Šคํƒ, failed set
85+
*/
86+
87+
//3. dp๋ฅผ ํ™œ์šฉํ•˜์—ฌ, ์ด์ค‘ ํฌ๋ฌธ์œผ๋กœ j,i ์‚ฌ์ด์— ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
88+
89+
var wordBreak = function(s, wordDict) {
90+
const wordSet = new Set(wordDict);
91+
const dp = new Array(s.length + 1).fill(false);
92+
dp[0] = true; // ๋นˆ ๋ฌธ์ž์—ด์€ ํ•ญ์ƒ ๊ฐ€๋Šฅ
93+
94+
for (let i = 1; i <= s.length; i++) {
95+
for (let j = 0; j < i; j++) {
96+
if (dp[j] && wordSet.has(s.slice(j, i))) {
97+
dp[i] = true;
98+
break; // ๋” ์ด์ƒ ๊ฒ€์‚ฌ ์•ˆ ํ•ด๋„ ๋จ
99+
}
100+
}
101+
}
102+
103+
return dp[s.length];
104+
};
105+
106+
/*
107+
์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
108+
๊ณต๊ฐ„๋ณต์žก๋„ : O(n)
109+
*/

0 commit comments

Comments
ย (0)