-
-
Notifications
You must be signed in to change notification settings - Fork 247
[Grapefruitgreentealoe] Week 5 Solutions #1852
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
[Grapefruitgreentealoe] Week 5 Solutions #1852
Conversation
let minPrice = Infinity; | ||
let maxProfit = 0; | ||
|
||
for (let price of prices) { |
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.
또는 const
도 괜찮겠네요.
|
||
for (let price of prices) { | ||
// 가장 싼 가격을 갱신 | ||
if (price < minPrice) { |
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.
또는 price - minPrice
를 전처리해서 재사용한다면 음수일 때이겠네요.
for (let char of str) { | ||
count[char.charCodeAt(0) - 97]++; | ||
} | ||
const key = count.join('#'); // 구분자 없으면 ["1","11"]과 ["11","1"] 같은 키로 오해 가능 |
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.
또는 leading zeros를 넣어도 되겠군요.
return Array.from(map.values()); | ||
}; | ||
|
||
//시간복잡도 : O(n * k). 정렬이 없으므로 |
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.
마찬가지로, Destructuring 연산 때문에 O(n * (k + n)) = O(n * k + n^2)
이 되겠습니다.
count[char.charCodeAt(0) - 97]++; | ||
} | ||
const key = count.join('#'); // 구분자 없으면 ["1","11"]과 ["11","1"] 같은 키로 오해 가능 | ||
map.set(key, [...(map.get(key) || []), str]); |
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.
map.set(key, [...(map.get(key) || []), str]); | |
map.set(key, map.get(key)??[]).push(str); |
마찬가지로, Destructuring을 피하면 O(n)
이 O(1)
로 줄겠습니다.
Nullish Coalescing Operator를 사용해도 좋겠습니다.
|
||
/* | ||
시간복잡도: O(n^2) + 메모이제이션으로 최적화 | ||
공간복잡도 : O(n) : 재귀스택, failed set |
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.
wordSet
때문에 m = wordDict.length
를 더해야 합니다: O(n + m)
}; | ||
|
||
/* | ||
시간복잡도: O(n^2) + 메모이제이션으로 최적화 |
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.
얼마나 최적화했는지 계산할 수 있습니다. slice
도 O(n)
이므로 총 O(n^3)
입니다.
|
||
/* | ||
시간복잡도: O(n^2) | ||
공간복잡도 : O(n) |
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.
마찬가지로, wordSet
때문에 m = wordDict.length
를 더해야 합니다: O(n + m)
}; | ||
|
||
/* | ||
시간복잡도: O(n^2) |
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.
마찬가지로, slice
도 O(n)
이므로 총 O(n^3)
입니다.
for (let j = 0; j < i; j++) { | ||
if (dp[j] && wordSet.has(s.slice(j, i))) { | ||
dp[i] = true; | ||
break; // 더 이상 검사 안 해도 됨 |
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.
가지치기(Pruning) 가능할 줄 몰랐는데 배워갑니다!
@yhkee0404 상세한 리뷰 감사합니다!! |
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!