本网页所有文字内容由 imapbox邮箱云存储,邮箱网盘, iurlBox网页地址收藏管理器 下载并得到。
ImapBox 邮箱网盘 工具地址: https://www.imapbox.com/download/ImapBox.5.5.1_Build20141205_CHS_Bit32.exe
PC6下载站地址:PC6下载站分流下载
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox 网页视频 工具地址: https://www.imapbox.com/download/ImovieBox4.7.0_Build20141115_CHS.exe
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
 
 
 
 
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
 Note:
 You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
用left[ i ]和right[ j ]分别表示第一次与第二卖出的收入,其中left[ i ]表示i个元素之前买入卖出股票所能赚的最多的钱,right[ i ]表示i个元素之后买入卖出股票所能赚的最多的钱,
下面为具体的实现 ps:可以同一天买入卖出
public class Solution {     public int maxProfit(int[] prices) {      if(prices.length<2) return 0;      int []left =new int[prices.length];      int []right = new int[prices.length];         int minVal = prices[0];         for(int i=1;i<prices.length;i++){          minVal = Math.min(minVal, prices[i]);          left[i] = Math.max(left[i-1], prices[i]-minVal);         }         int maxVal = prices[prices.length-1];         for(int i=prices.length-2;i>=0;i--){          maxVal = Math.max(maxVal, prices[i]);          right[i] = Math.max(right[i+1], maxVal -prices[i]);         }         int res =  left[0]+right[0];         for(int i=1;i<prices.length;i++){          res = Math.max(res, left[i]+right[i]);         }         return res;     } }
阅读和此文章类似的: 程序员专区
官方软件产品操作指南 (170)