假设你有一个数组,其中第
个元素表示某只股票在第
天的价格。
设计一个算法来寻找最大的利润。你可以完成任意数量的交易(例如,多次购买和出售股票的一股)。但是,你不能同时进行多个交易(即,你必须在再次购买之前卖出之前买的股票)。
设计一个算法来寻找最大的利润。你可以完成任意数量的交易(例如,多次购买和出售股票的一股)。但是,你不能同时进行多个交易(即,你必须在再次购买之前卖出之前买的股票)。
[1,4,2]
3
第一天买入,第二天卖出,收益为4-1=3。
[1,2,1,4]
4
第一天买入,第二天卖出,第三天买入,第四天卖出,收益为(2-1)+(4-1)=4。
//在自己电脑上运行结果正确,但是在平台上运行保错请检查是否存在数组越界等非法访问情况case通过率为0.00% Exception in thread "main" java.lang.StackOverflowError at Solution.getProfit(Solution.java:33)import java.util.*; public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ static int maxP=0; static public int maxProfit (int[] prices) { // write code here getProfit(prices,0,false,0);//初始化buy=false未购买。cost=赚的钱 return maxP; } static public int getProfit(int[] prices, int day,boolean buy,int cost){ if(day==prices.length-1){//假设还有一天买卖的机会 if(buy){ cost=cost+prices[day];//一定会卖出去 } if(cost>maxP) maxP=cost; return cost; } int cost1,cost2; //遍历所有的情况 if(buy){//如果已经买了,则可能卖,也可能不卖。 cost1=getProfit(prices,day+1,false,cost+prices[day]);//卖出去 cost2=getProfit(prices,day+1,true,cost);//没有卖 } else{//如果还没买,可能买也可能不买。 cost1=getProfit(prices,day+1,false,cost);//没有买 cost2=getProfit(prices,day+1,true,cost-prices[day]);//买了 } return Math.max(cost1,cost2); } }
public class Solution {
public int maxProfit(int[] prices) {
//不限次数的交易,最大利润就是每次买入都是涨价的,所有涨的都有份,跌的时候早就卖了
int max=0;
for(int i=1;i<prices.length;i++){
if(prices[i]>prices[i-1])
max += prices[i]-prices[i-1];
}
return max;
}
}
class Solution {
/**
* 继续上一题思路,继续用转移方程
* 假设第i天持有股票的收益为f(i,1),未持有的收益为f(i,0)
* f(i,1) = max(f(i-1, 1), f(i-1, 0)-prices[i])
* f(i,0) = max(f(i-1, 0), f(i-1, 1)+prices[i])
* f(0,0) = 0, f(0,1)=-prices[0];
* 还有一个思路是遍历判断前后两天的收益,是正的就累计
* 因为不限制交易次数,可以在跌了前卖出,涨之前买入
* 吃掉所有利差
*/
public int maxProfit(int[] prices) {
if(prices.length<2) return 0;
int i0=0, i1=-prices[0];
for(int i=0; i<prices.length; i++){
int j0 = Math.max(i0, i1+prices[i]);
int j1 = Math.max(i1, i0-prices[i]);
i0=j0;
i1=j1;
}
return i0;
}
} public class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i] < prices[i + 1]) {
sum += (prices[i + 1] - prices[i]);
}
}
return sum;
}
}
public class Solution {
//相邻两天的股票价格如果是递增关系,就第一天买入,第二天就卖出,这样可以得到累积获得的利润最大
public int maxProfit(int[] prices) {
if(prices.length < 2)
return 0;
int max_profit = 0;
int buy = prices[0];
for(int i = 1; i < prices.length; i++){
if(prices[i] > buy){
max_profit += prices[i] - buy;
}
buy = prices[i];
}
return max_profit;
}
}
如果数组递增,如1,3,5,则可以看做连续买入,也就是5-1=5-3+3-1。
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0)
return 0;
int result = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] >= prices[i-1]) {
result += prices[i] - prices[i-1];
}
}
return result;
}
}
if (prices.length <= 1) {
return 0;
}
int min = prices[0];
int max = prices[0];
int profit = 0;
for (int i : prices
) {
if (i < max) {
profit += max - min;
max = i;
min = i;
}
if (i < min) {
// buy day
min = i;
}
if (i > max) {
// sell day
max = i;
}
}
if (max>min)
profit += max - min;
return profit; //一开始题目都没读懂,23333
public class Solution {
public int maxProfit(int[] prices) {
//可以买卖多次,波谷买入,波峰卖出,即可实现利益最大化
if (prices == null || prices.length < 2)
return 0;
int sum = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1])
sum += (prices[i] - prices[i - 1]);
}
return sum;
}
}
//因为题目中没有限定买卖次数,所以这里只要有收益就选择卖出,再买。就可以!
public class Solution {
public int maxProfit(int[] prices) {
int n=prices.length;
int currSum=0;
int maxSum=0;
for(int i=1;i<n;i++){
currSum=Math.max(currSum,currSum+prices[i]-prices[i-1]);
maxSum=Math.max(currSum,maxSum);
}
return maxSum;
}
}
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1)
return 0;
int maxProfit = 0;
int temp = 0;
for (int i = 1; i < prices.length; ++i) {
temp = prices[i] - prices[i - 1];
if (temp > 0)
maxProfit += temp;
}
return maxProfit;
}
} public class Solution {
public int maxProfit(int[] prices) {
int max = 0;
for (int i = 1; i < prices.length; i++) max += Math.max(0, prices[i] - prices[i-1]);
return max;
}
}
public class Solution {//所有递增子区间
public int maxProfit(int[] prices) {
int res=0;
int index=0;
for(int i=1;i<prices.length;i++){
if(prices[i]<prices[i-1]){
res+=(prices[i-1]-prices[index]);
index=i;
}
if(i==prices.length-1){
res+=(prices[i]-prices[index]);
}
}
return res;
}
}