Skip to content

[Yn3-3xh] WEEK 05 Solutions #1402

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/Yn3-3xh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
[문제풀이]
- 작은수를 두고, 큰수에 뺀 값을 구하자.
time: O(N), space: O(1)

[회고]
이번 문제는 난이도가 easy인 덕분에 무리없이 풀었던 것 같다.
*/
class Solution {
public int maxProfit(int[] prices) {
int min = prices[0];
int max = 0;
for (int i = 1; i < prices.length; i++) {
min = Math.min(min, prices[i]);
max = Math.max(max, prices[i] - min);
}
return max;
}
}