Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
21 lines (20 loc) · 696 Bytes

File metadata and controls

21 lines (20 loc) · 696 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package leetcode.P121;
public class Solution3 {
/**
* 매번 현재의 가격이 최소 가격인지 확인. 그렇지 않으면 최대 이익을 계산한다.
* 한 번의 순회로 문제를 해결할 수 있게 된다.
* O(N)의 시간 복잡도를 가진다.
*/
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
} else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.