-
-
Notifications
You must be signed in to change notification settings - Fork 248
[gomgom22] Week5 #877
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
[gomgom22] Week5 #877
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fcf70c9
add: solve #221 Best Time to Buy And Sell Stock with ts
Yjason-K 2236c56
add: solve #236 Group Anagrams with ts
Yjason-K 6bed3de
add: solve #238 Encode and Decode strings with ts
Yjason-K de48951
add: solve #271 Word Break with ts
Yjason-K 79ca2b9
fix: line break
Yjason-K 865210d
fix: line break
Yjason-K 7b26180
fix: 공백 제거
Yjason-K 928c097
fix: 오탈자 수정
Yjason-K 9260d77
fix: 공백 제거
Yjason-K File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* 최대 이익을 계산하는 함수 | ||
* @param {number[]} prices | ||
* @returns {number} | ||
* | ||
* 시간 복잡도 : O(n) (n: 주식 가격 배열의 길이) | ||
* 공간 복잡도 : 0(1) (추가 자료구조 X) | ||
*/ | ||
function maxProfit(prices: number[]): number { | ||
let minPrice = 100001; // 지금까지의 최소 가격 | ||
let maxProfit = 0; // 최대 이익 | ||
|
||
for (let price of prices) { | ||
// 최소 가격 갱신 | ||
if (price < minPrice) { | ||
minPrice = price; | ||
} | ||
|
||
// 현재 가격에서 최소 가격을 뺀 이익이 최대 이익보다 크다면 갱신 | ||
const potentialProfit = price - minPrice; | ||
if (potentialProfit > maxProfit) { | ||
maxProfit = potentialProfit; | ||
} | ||
} | ||
|
||
return maxProfit; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @description 문자열 배열을 하나의 문자열로 인코딩합니다. | ||
* @param {string[]} strs - 문자열 배열 | ||
* @returns {string} 인코딩된 문자열 | ||
* | ||
* 시간 복잡도: O(N) | ||
* - N은 입력 배열의 모든 문자열 길이의 합 | ||
* 공간 복잡도: O(1) | ||
* - 추가 메모리 사용 없음 | ||
*/ | ||
function encode(strs: string[]): string { | ||
return strs.join(':'); | ||
} | ||
|
||
/** | ||
* @description 인코딩된 문자열을 다시 문자열 배열로 디코딩합니다. | ||
* @param {string} s - 인코딩된 문자열 | ||
* @returns {string[]} 디코딩된 문자열 배열 | ||
* | ||
* 시간 복잡도: O(N) | ||
* - N은 입력 문자열의 길이 | ||
* 공간 복잡도: O(1) | ||
* - 추가 메모리 사용 없음 | ||
*/ | ||
function decode(s: string): string[] { | ||
return s.split(':'); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* 주어진 문자열 배열에서 Anagram을 그룹화 해서 배열 만들기 | ||
* @param {string[]} strs - 문자열 배열 | ||
* @returns {string[][]} Anagram을 그룹 배열 | ||
* | ||
* 문자열들을 정렬해서 Map에 담아서 존재하면 그 때의 문자를 value로 추가 | ||
* 존재하지 않으면 새로운 배열을 value로 추가 | ||
* | ||
* 시간 복잡도: O(N * M * log(M)) | ||
* - N은 문자열 배열의 길이 | ||
* - M은 각 문자열의 평균 길이 (정렬 시간 때문) | ||
* 공간 복잡도: O(N * M) | ||
* - 해시맵에 저장되는 문자열 그룹 때문 | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const anagramMap: Map<string, string[]> = new Map(); | ||
for (const str of strs) { | ||
|
||
// 정렬된 문자열 | ||
const sortedStr = str.split('').sort().join(''); | ||
|
||
// 정렬된 문자열이 존재하는 않는 경우 | ||
if (!anagramMap.has(sortedStr)) { | ||
anagramMap.set(sortedStr, []) | ||
} | ||
|
||
// 정렬된 문자열을 key로 하는 value str 추가 | ||
anagramMap.get(sortedStr)?.push(str); | ||
} | ||
|
||
// anagramMap에서 values만 배열로해서 출력 | ||
return Array.from(anagramMap.values()) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* 주어진 문자열이 단어 사전에 있는 단어들로 나누어질 수 있는지 확인합니다. | ||
* | ||
* | ||
* @param {string} s - 확인할 문자열 | ||
* @param {string[]} wordDict - 단어 사전 | ||
* @returns {boolean} 문자열이 단어로 완벽히 나누어질 수 있으면 `true`, 아니면 `false` | ||
* | ||
* 시간 복잡성 O(n * m * k) | ||
* - n: 문자열 s 길이 | ||
* - m: 단어 사전 길이 | ||
* - k: 단어 사전 내 단어 길이 | ||
* | ||
* 공간 복잡성 O(n) | ||
* - 메모이제이션(memo) 및 재귀 호출 스택 크기가 문자열 길이 n에 비례. | ||
*/ | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
const memo: Record<number, boolean> = {}; | ||
|
||
/** | ||
* @param {number} start - 현재 시작 idx | ||
* @returns {boolean} 주어진 idx 부터 문자열을 나눌 수 있으면 true | ||
*/ | ||
const dfs = (start: number): boolean => { | ||
if (start in memo) return memo[start]; | ||
if (start === s.length) return (memo[start] = true); | ||
|
||
for (const word of wordDict) { | ||
if (s.startsWith(word, start) && dfs(start + word.length)) { | ||
return (memo[start] = true); | ||
} | ||
} | ||
|
||
return (memo[start] = false); | ||
}; | ||
|
||
return dfs(0); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.