-
-
Notifications
You must be signed in to change notification settings - Fork 195
[clara-shin] WEEK 05 solutions #1392
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
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a48ed05
Best Time to Buy and Sell Stock solution
clara-shin a01fdcf
update Best Time to Buy and Sell Stock solution
clara-shin 830a03e
Group Anagrams solution
clara-shin aeabb3d
Word Search solution
clara-shin 6530354
Encode and Decode Strings solution
clara-shin 2c6c1d7
Implement Trie (Prefix Tree) solution
clara-shin aaeb0b1
Word Break solution
clara-shin 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,33 @@ | ||
/** | ||
* 시간 복잡도 O(n) | ||
* 공간 복잡도 O(1) | ||
* | ||
* 그리디 알고리즘 | ||
* 현재까지의 최저 가격을 기억하고, 그 가격에 샀을 때의 이익을 계속 계산하여 최대 이익을 구함 | ||
*/ | ||
|
||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function (prices) { | ||
let minPrice = prices[0]; // 최저 가격 초기화 (첫 날 가격) | ||
let maxProfit = 0; // 최대 이익 초기화 (아직 이익 없음) | ||
|
||
// 두 번째 날부터 | ||
for (let i = 1; i < prices.length; i++) { | ||
// 현재 가격이 최저 가격보다 낮으면 최저 가격 업데이트 | ||
if (prices[i] < minPrice) { | ||
minPrice = prices[i]; | ||
} | ||
// 현재 가능한 이익 계산 (현재 가격 - 최저 가격) | ||
const currentProfit = prices[i] - minPrice; | ||
|
||
// 최대 이익 업데이트 | ||
if (currentProfit > maxProfit) { | ||
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. Math.max 함수를 쓰면 직관적이고, 한줄로 코드량도 줄어들것 같아요! |
||
maxProfit = currentProfit; | ||
} | ||
} | ||
|
||
return maxProfit; | ||
}; |
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.
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.
Math.min 함수를 쓰면 직관적이고, 한줄로 코드량도 줄어들것 같아요!
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.
@ayosecu
그러네요! 내장함수를 사용하니 더 간결하고 의도가 명확히 보여요 감사합니다!