-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[sukyoungshin] WEEK 05 #1408
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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; | ||
} | ||
}; |
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()]; | ||
}; |
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오! 뭔가 DP와 재귀 구조가 섞여있는 알고리즘인 것 같아요!
제가 제대로 이해한게 맞을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 맞아요! left 쪽 문자열을 앞에서부터 하나씩 늘려가면서 wordDict에 포함되는지 확인하고, |
||
cache[str] = true; | ||
return true; | ||
} | ||
} | ||
|
||
cache[str] = false; | ||
return false; | ||
} | ||
|
||
return canSplit(s); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전역적으로 가장 주식 가격이 가장 낮았던 가격을 유지하면서 n번째 날짜와의 거래가 비교로 가장 큰 수익을 찾는 방법이군요!
직관적으로 읽혀서 좋은것 같아요
(저는 카데인 알고리즘으로 끙끙 앓으면서 풀었던 기억이 나는데,, 배워갑니다!🙏🙏)