Skip to content

[sukyoungshin] WEEK 05 #1408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions best-time-to-buy-and-sell-stock/sukyoungshin.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전역적으로 가장 주식 가격이 가장 낮았던 가격을 유지하면서 n번째 날짜와의 거래가 비교로 가장 큰 수익을 찾는 방법이군요!

직관적으로 읽혀서 좋은것 같아요
(저는 카데인 알고리즘으로 끙끙 앓으면서 풀었던 기억이 나는데,, 배워갑니다!🙏🙏)

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function maxProfit(prices: number[]): number {
let minPrice = Number.MAX_SAFE_INTEGER;
let maxProfit = 0;

for (let i = 0; i < prices.length; i++) {
const currentPrice = prices[i];

if (currentPrice < minPrice) {
minPrice = currentPrice;
} else {
const profit = currentPrice - minPrice;
maxProfit = Math.max(maxProfit, profit);
}
}

return maxProfit;
};

maxProfit([7, 1, 5, 3, 6, 4]); // Output: 5
maxProfit([7, 6, 4, 3, 1]); // Output: 0
maxProfit([2, 4, 1]); // Output: 2
maxProfit([1, 2]); // Output: 1
maxProfit([2, 1]); // Output: 0
maxProfit([1, 2, 3, 4, 5]); // Output: 4
41 changes: 41 additions & 0 deletions encode-and-decode-strings/sukyoungshin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// https://neetcode.io/problems/string-encode-and-decode

class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
let encodedStrings: string[] = [];

for (const word of strs) {
const length = word.length;
encodedStrings.push(`${length}#${word}`);
}

return encodedStrings.join("");
}

/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
const decodedStrings: string[] = [];
let position = 0;

while (position < str.length) {
const hashIndex = str.indexOf("#", position);
const length = Number(str.slice(position, hashIndex));

const start = hashIndex + 1;
const end = start + length;
const word = str.slice(start, end);
decodedStrings.push(word);

position = end;
}

return decodedStrings;
}
};
31 changes: 31 additions & 0 deletions group-anagrams/sukyoungshin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 1. 객체사용
function groupAnagrams1(strs: string[]): string[][] {
let anagramGroups: Record<string, string[]> = {};
for (const word of strs) {
const sortedKey = [...word].sort().join("");

if (sortedKey in anagramGroups) {
anagramGroups[sortedKey].push(word);
} else {
anagramGroups[sortedKey] = [word];
}
}

return Object.values(anagramGroups);
};

// 2. Map 사용
function groupAnagrams2(strs: string[]): string[][] {
let anagramGroups = new Map<string, string[]>();
for (const word of strs) {
const key = [...word].sort().join("");

if (anagramGroups.has(key)) {
anagramGroups.get(key)?.push(word);
} else {
anagramGroups.set(key, [word]);
}
}

return [...anagramGroups.values()];
};
23 changes: 23 additions & 0 deletions word-break/sukyoungshin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function wordBreak(s: string, wordDict: string[]): boolean {
const cache: Record<string, boolean> = {};

function canSplit(str: string): boolean {
if (str === "") return true;
if (cache[str] !== undefined) return cache[str];

for (let i = 1; i <= str.length; i++) {
const left = str.slice(0, i);
const right = str.slice(i);

if (wordDict.includes(left) && canSplit(right)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오! 뭔가 DP와 재귀 구조가 섞여있는 알고리즘인 것 같아요!

left 변수에 문자를 하나 씩 추가하면서 wordDict 포함되는지 검사하면서, 나머지 문자열에 대해 right 변수에 재귀적으로 넘겨서 확인하는 것 같은데

제가 제대로 이해한게 맞을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 맞아요! left 쪽 문자열을 앞에서부터 하나씩 늘려가면서 wordDict에 포함되는지 확인하고,
나머지 문자열(right)은 재귀적으로 확인하는 구조예요.

cache[str] = true;
return true;
}
}

cache[str] = false;
return false;
}

return canSplit(s);
};