Skip to content

Commit df66dc6

Browse files
committed
feat(soobing): week5 > maxProfit
1 parent 980e610 commit df66dc6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 문제 유형
3+
* - Array
4+
*
5+
* 문제 설명
6+
* - 주식을 가장 싸게 사서 비싸게 팔수 있는 경우 찾기
7+
*
8+
* 아이디어
9+
* 1) 최소값을 찾고 그 이후의 값 중 최대값을 찾는다.
10+
*
11+
*/
12+
function maxProfit(prices: number[]): number {
13+
let min = prices[0];
14+
let maxProfit = 0;
15+
16+
for (let i = 1; i < prices.length; i++) {
17+
min = Math.min(min, prices[i]);
18+
maxProfit = Math.max(prices[i] - min, maxProfit);
19+
}
20+
return maxProfit;
21+
}

0 commit comments

Comments
 (0)