Skip to content

Commit 4bbd6b7

Browse files
authored
Merge pull request #1408 from sukyoungshin/main
[sukyoungshin] WEEK 05
2 parents 6451a3b + db5924b commit 4bbd6b7

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Number.MAX_SAFE_INTEGER;
3+
let maxProfit = 0;
4+
5+
for (let i = 0; i < prices.length; i++) {
6+
const currentPrice = prices[i];
7+
8+
if (currentPrice < minPrice) {
9+
minPrice = currentPrice;
10+
} else {
11+
const profit = currentPrice - minPrice;
12+
maxProfit = Math.max(maxProfit, profit);
13+
}
14+
}
15+
16+
return maxProfit;
17+
};
18+
19+
maxProfit([7, 1, 5, 3, 6, 4]); // Output: 5
20+
maxProfit([7, 6, 4, 3, 1]); // Output: 0
21+
maxProfit([2, 4, 1]); // Output: 2
22+
maxProfit([1, 2]); // Output: 1
23+
maxProfit([2, 1]); // Output: 0
24+
maxProfit([1, 2, 3, 4, 5]); // Output: 4
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// https://neetcode.io/problems/string-encode-and-decode
2+
3+
class Solution {
4+
/**
5+
* @param {string[]} strs
6+
* @returns {string}
7+
*/
8+
encode(strs) {
9+
let encodedStrings: string[] = [];
10+
11+
for (const word of strs) {
12+
const length = word.length;
13+
encodedStrings.push(`${length}#${word}`);
14+
}
15+
16+
return encodedStrings.join("");
17+
}
18+
19+
/**
20+
* @param {string} str
21+
* @returns {string[]}
22+
*/
23+
decode(str) {
24+
const decodedStrings: string[] = [];
25+
let position = 0;
26+
27+
while (position < str.length) {
28+
const hashIndex = str.indexOf("#", position);
29+
const length = Number(str.slice(position, hashIndex));
30+
31+
const start = hashIndex + 1;
32+
const end = start + length;
33+
const word = str.slice(start, end);
34+
decodedStrings.push(word);
35+
36+
position = end;
37+
}
38+
39+
return decodedStrings;
40+
}
41+
};

group-anagrams/sukyoungshin.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 1. 객체사용
2+
function groupAnagrams1(strs: string[]): string[][] {
3+
let anagramGroups: Record<string, string[]> = {};
4+
for (const word of strs) {
5+
const sortedKey = [...word].sort().join("");
6+
7+
if (sortedKey in anagramGroups) {
8+
anagramGroups[sortedKey].push(word);
9+
} else {
10+
anagramGroups[sortedKey] = [word];
11+
}
12+
}
13+
14+
return Object.values(anagramGroups);
15+
};
16+
17+
// 2. Map 사용
18+
function groupAnagrams2(strs: string[]): string[][] {
19+
let anagramGroups = new Map<string, string[]>();
20+
for (const word of strs) {
21+
const key = [...word].sort().join("");
22+
23+
if (anagramGroups.has(key)) {
24+
anagramGroups.get(key)?.push(word);
25+
} else {
26+
anagramGroups.set(key, [word]);
27+
}
28+
}
29+
30+
return [...anagramGroups.values()];
31+
};

word-break/sukyoungshin.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function wordBreak(s: string, wordDict: string[]): boolean {
2+
const cache: Record<string, boolean> = {};
3+
4+
function canSplit(str: string): boolean {
5+
if (str === "") return true;
6+
if (cache[str] !== undefined) return cache[str];
7+
8+
for (let i = 1; i <= str.length; i++) {
9+
const left = str.slice(0, i);
10+
const right = str.slice(i);
11+
12+
if (wordDict.includes(left) && canSplit(right)) {
13+
cache[str] = true;
14+
return true;
15+
}
16+
}
17+
18+
cache[str] = false;
19+
return false;
20+
}
21+
22+
return canSplit(s);
23+
};

0 commit comments

Comments
 (0)