Best Time to Buy and Sell Stock 주식 매매 문제의 몇 가지 변형
LeetCode 121. Best Time to Buy and Sell Stock
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
class Solution {
public int maxProfit(int[] prices) {
if(prices.length < 2) {
return 0;
}
int profit = 0;
// two iteration solution, time limitation exceed
/*
for(int i = 0; i < prices.length - 1; i++) {
for(int j = i + 1; j < prices.length; j++) {
if(prices[j] - prices[i] > profit) {
profit = prices[j] - prices[i];
}
}
}
*/
int buyIdx = 0;
int sellIdx = 1;
while(sellIdx < prices.length) {
if(prices[buyIdx] > prices[sellIdx]) {
buyIdx = sellIdx;
} else {
int t = prices[sellIdx] - prices[buyIdx];
if(t > profit) {
profit = t;
}
}
sellIdx++;
}
return profit;
}
}
LeetCode 122. Best Time to Buy and Sell Stock II
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
// null pointer check
if(prices == null) {
return profit;
}
for(int i = 0; i < prices.length - 1; i++) {
if(prices[i + 1] > prices[i]) {
profit = profit + (prices[i + 1] - prices[i]);
}
}
return profit;
}
}
LeetCode 123. Best Time to Buy and Sell Stock III
Design an algorithm to find the maximum profit. You may complete at most two transactions.
class Solution {
public int maxProfit(int[] prices) {
int buy1 = Integer.MAX_VALUE;
int buy2 = Integer.MAX_VALUE;
int sell1 = 0;
int sell2 = 0;
for (int i = 0; i < prices.length; i++) {
buy1 = Math.min(buy1, prices[i]);
sell1 = Math.max(sell1, prices[i] - buy1);
buy2 = Math.min(buy2, prices[i] - sell1);
sell2 = Math.max(sell2, prices[i] - buy2);
}
return sell2;
}
}
LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length < 2) return 0;
// DP - interative solution
// profit1[i]: the profit if sell on day i
// profit2[i]: the profit if do nothing on day i
int[] profit1 = new int[prices.length];
int[] profit2 = new int[prices.length];
profit1[0] = 0;
profit2[0] = 0;
for(int i = 1; i < prices.length; i++) {
profit1[i] = Math.max(profit1[i - 1] + prices[i] - prices[i - 1], profit2[i - 1]);
profit2[i] = Math.max(profit1[i - 1], profit2[i - 1]);
}
return Math.max(profit1[profit1.length - 1], profit2[profit2.length - 1]);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.