Best Time to Buy and Sell Stock II
LeetCode 122. Best Time to Buy and Sell Stock II
원문
You are given an integer array prices
where prices[i]
is the price of a given stock on the i
th
day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
발상
i
일에 사서j
일에 팔 때의 수익을 2차원 그래프로 나타내보고 거기서 규칙을 찾아보자.- 장렬히 실패
도움!
고민 끝에 Discussion
섹션을 눌렀는데 어떤 분이 아주 친절하게도 적절한 힌트를 주셨더라.
1
2
We want to buy all the stocks when the line is going up. And want to ignore all the lines when the line is going down.
(생략)
이 말대로 상승하는 경우만 더했다.
코드
Python
1
2
3
4
5
6
7
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
for i in range(len(prices) - 1):
if prices[i] < prices[i + 1]:
profit += prices[i + 1] - prices[i]
return profit
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i] < prices[i + 1]) {
profit += prices[i + 1] - prices[i];
}
}
return profit;
}
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.