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
16 lines (15 loc) · 593 Bytes

File metadata and controls

16 lines (15 loc) · 593 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
package ch07;
public class P12_2 {
public int maxProfit(int[] prices) {
// 최대 이익은 0, 저점은 임시로 첫 번째 값으로 지정
int maxProfit = 0, minPrice = prices[0];
// 현재 값을 우측으로 차례대로 이동하면서 계산
for (int price : prices) {
// 지금까지 저점 계산
minPrice = Math.min(minPrice, price);
// 현재 값과 저점의 차이가 최대 이익인 경우 교체
maxProfit = Math.max(maxProfit, price - minPrice);
}
return maxProfit;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.