Skip to content

Commit b6c8fd4

Browse files
authored
Merge pull request #2174 from Blossssom/main
[Blossssom] WEEK 05 solutions
2 parents 160d1d8 + a35c403 commit b6c8fd4

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxProfit(prices: number[]): number {
2+
let min = prices[0];
3+
let max = 0;
4+
5+
for (let i = 0; i < prices.length; i++) {
6+
min = Math.min(min, prices[i]);
7+
max = Math.max(max, prices[i] - min);
8+
}
9+
10+
return max;
11+
}
12+
13+
const prices = [3, 3, 5, 0, 0, 3, 1, 4];
14+
maxProfit(prices);
15+
16+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
private baseStr = "#";
3+
/*
4+
* @param strs: a list of strings
5+
* @return: encodes a list of strings to a single string.
6+
*/
7+
public encode(strs: string[]): string {
8+
let encodedStr = "";
9+
for (const str of strs) {
10+
encodedStr += `${str.length}${this.baseStr}${str}`;
11+
}
12+
return encodedStr;
13+
}
14+
15+
/*
16+
* @param str: A string
17+
* @return: decodes a single string to a list of strings
18+
*/
19+
public decode(str: string): string[] {
20+
const spliter = [];
21+
let idx = 0;
22+
while (idx < str.length) {
23+
let findBase = idx;
24+
// length๊ฐ€ 10 ์ด์ƒ์ผ ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด ์ˆœํšŒํ•˜๋ฉฐ ํŒŒ์‹ฑ
25+
while (str[findBase] !== this.baseStr) {
26+
findBase++;
27+
}
28+
29+
const contentLength = parseInt(str.substring(idx, findBase));
30+
const content = str.substring(findBase + 1, findBase + contentLength + 1);
31+
spliter.push(content);
32+
idx = findBase + contentLength + 1;
33+
}
34+
return spliter;
35+
}
36+
}
37+
38+
const input = ["lint", "code", "love", "you"];
39+
40+
const solutionInstance = new Solution();
41+
42+
const encoded = solutionInstance.encode(input);
43+
const decoded = solutionInstance.decode(encoded);
44+
45+
console.log(encoded, decoded);
46+
47+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param strs - ๋ฌธ์ž์—ด ๋ฐฐ์—ด
3+
* @returns - anagram์ด ๊ฐ€๋Šฅํ•œ ์ด์ค‘ ๋ฐฐ์—ด
4+
* @description
5+
* - ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 10^4๋กœ ์ˆœํšŒ๋Š” ๊ฐ€๋Šฅํ•œ ์ ๊ฒŒ
6+
* - ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 100์œผ๋กœ ๋„๋„
7+
* - 1. ๋‚ด๋ถ€ ๋ฌธ์ž์—ด ์ •๋ ฌ (O(N * K log K))
8+
* - 2. ์•„์Šคํ‚ค๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ 1๋ฒˆ๋งŒ ์ˆœํšŒ (O(N * K))
9+
*
10+
* - ๋‹ค๋งŒ ๋ฌธ์ž์—ด์ด ์ž‘์„ ๊ฒฝ์šฐ (ํ˜„์žฌ 100 ์ดํ•˜๋กœ ์ž‘์€ํŽธ) ์ •๋ ฌ๋ณด๋‹ค ๋ฐฐ์—ด ์ƒ์„ฑ๊ณผ ๋ฌธ์ž์—ด join์ด ๋” ๋ฌด๊ฑฐ์›Œ ์งˆ ์ˆ˜ ์žˆ์Œ
11+
* - Map์„ ์“ฐ๋Š”๊ฒŒ ์กฐ๊ธˆ์€ ๋” ๋น ๋ฅผ๋“ฏ
12+
*/
13+
14+
// function groupAnagrams(strs: string[]): string[][] {
15+
// const strObj: Record<string, string[]> = {};
16+
// for (let i = 0; i < strs.length; i++) {
17+
// const sortedItem = strs[i].split("").sort().join("");
18+
// strObj[sortedItem] ??= [];
19+
// strObj[sortedItem].push(strs[i]);
20+
// }
21+
22+
// return Object.values(strObj);
23+
// }
24+
25+
function groupAnagrams(strs: string[]): string[][] {
26+
const strObj: Record<string, string[]> = {};
27+
for (let i = 0; i < strs.length; i++) {
28+
const alpha = Array.from({ length: 26 }, () => 0);
29+
30+
for (const st of strs[i]) {
31+
alpha[st.charCodeAt(0) - "a".charCodeAt(0)]++;
32+
}
33+
34+
const joinStr = alpha.join(",");
35+
strObj[joinStr] ??= [];
36+
strObj[joinStr].push(strs[i]);
37+
}
38+
39+
return Object.values(strObj);
40+
}
41+
42+
const strs = ["a"];
43+
44+
groupAnagrams(strs);
45+
46+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// ๋‹จ์ˆœ ๊ตฌํ˜„ ์‹œ ํšจ์œจ์ด ๋‚ฎ๊ฒŒ ๋‚˜์˜ด
2+
// class Trie {
3+
// private word: Set<string>;
4+
// constructor() {
5+
// this.word = new Set();
6+
// }
7+
8+
// insert(word: string): void {
9+
// this.word.add(word);
10+
// }
11+
12+
// search(word: string): boolean {
13+
// return this.word.has(word);
14+
// }
15+
16+
// startsWith(prefix: string): boolean {
17+
// for (const str of this.word) {
18+
// if (prefix.length > str.length) {
19+
// continue;
20+
// }
21+
// let check = true;
22+
// for (let i = 0; i < prefix.length; i++) {
23+
// if (str[i] !== prefix[i]) {
24+
// check = false;
25+
// break;
26+
// }
27+
// }
28+
// if (check) {
29+
// return true;
30+
// }
31+
// }
32+
// return false;
33+
// }
34+
// }
35+
36+
// ๋…ธ๋“œ ๊ตฌ์กฐ๋ฅผ ๊ฐœ์„ 
37+
class TrieNode {
38+
children: { [key: string]: TrieNode };
39+
isEndOfWord: boolean;
40+
constructor() {
41+
this.children = {};
42+
this.isEndOfWord = false;
43+
}
44+
}
45+
46+
class Trie {
47+
private root: TrieNode;
48+
constructor() {
49+
this.root = new TrieNode();
50+
}
51+
52+
insert(word: string): void {
53+
// ๊ธ€์ž ํ•˜๋‚˜์”ฉ TrieNode ํƒ€์ž…์œผ๋กœ ์ €์žฅ
54+
let currentNode = this.root;
55+
for (const char of word) {
56+
if (!currentNode.children[char]) {
57+
currentNode.children[char] = new TrieNode();
58+
}
59+
60+
currentNode = currentNode.children[char];
61+
}
62+
currentNode.isEndOfWord = true;
63+
console.log(this.root);
64+
}
65+
66+
// ๊ฐ ๊ธ€์ž๋ฅผ TrieNode์—์„œ ์„œ์นญํ•˜๋ฉฐ ํŒ๋‹จ
67+
search(word: string): boolean {
68+
let currentNode = this.root;
69+
for (const char of word) {
70+
if (!currentNode.children[char]) {
71+
return false;
72+
}
73+
currentNode = currentNode.children[char];
74+
}
75+
return currentNode.isEndOfWord;
76+
}
77+
78+
// ๋™์ผ ๋กœ์ง์ด์ง€๋งŒ prefix ๋งŒํผ๋งŒ ์กด์žฌํ•˜๋ฉด true
79+
startsWith(prefix: string): boolean {
80+
let currentNode = this.root;
81+
for (const char of prefix) {
82+
if (!currentNode.children[char]) {
83+
return false;
84+
}
85+
currentNode = currentNode.children[char];
86+
}
87+
return true;
88+
}
89+
}
90+
91+
const trie = new Trie();
92+
trie.insert("apple");
93+
trie.search("apple"); // true
94+
trie.search("app"); // false
95+
trie.startsWith("app"); // true
96+
trie.insert("app");
97+
trie.search("app"); // true
98+
99+

โ€Žword-break/Blossssom.tsโ€Ž

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param s - ํƒ€๊ฒŸ ๋ฌธ์ž์—ด
3+
* @param wordDict - ๋ฌธ์ž์—ด ๋ฐฐ์—ด
4+
* @returns - wordDict์˜ ์š”์†Œ๋ฅผ ์กฐํ•ฉํ•ด ํƒ€๊ฒŸ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ return
5+
* @description
6+
* - wordDict์˜ ์š”์†Œ๋Š” ์ค‘๋ณต ์‚ฌ์šฉ ๊ฐ€๋Šฅ
7+
* - dp ๊นŒ์ง„ ์ƒ๊ฐํ–ˆ์ง€๋งŒ ์„ธ๋ถ€ ๊ตฌํ˜„๊นŒ์ง„ ํž˜๋“ค์—ˆ์Œ
8+
*/
9+
10+
function wordBreak(s: string, wordDict: string[]): boolean {
11+
// ๊ฐ ์š”์†Œ๋ณ„ ๋‚จ์€ ๋‹จ์–ด๋ฅผ ์ €์žฅํ•œ๋‹ค๋ฉด?
12+
const dictSet = new Set(wordDict);
13+
const dp = Array.from({ length: s.length + 1 }, () => false);
14+
15+
dp[0] = true;
16+
17+
for (let i = 1; i <= s.length; i++) {
18+
for (let j = 0; j < i; j++) {
19+
if (dp[j] && dictSet.has(s.slice(j, i))) {
20+
dp[i] = true;
21+
break;
22+
}
23+
}
24+
}
25+
26+
return dp[s.length];
27+
}
28+
29+
const s = "catsandog";
30+
const wordDict = ["cats", "dog", "sand", "and", "cat"];
31+
32+
const answer = wordBreak(s, wordDict);
33+
console.log("is answer :", answer);
34+
35+

0 commit comments

Comments
ย (0)