找到最低值和最高值

int maxProfit(vector<int>& prices) {
if(prices.size()<)return ;
int profit=;
int cur_min=prices[];
for(int i=;i<prices.size();i++)
{
profit=max(profit,prices[i]-cur_min);//记录最大利润
cur_min=min(cur_min,prices[i]);//保留购买最小值
}
return profit;
}

2、

计算差分序列,大于0加入

int maxProfit(vector<int>& prices) {
if(prices.size()<)return ;
int profit=;
int diff=;
for(int i=;i<prices.size();i++)
{
diff=prices[i]-prices[i-];
if(diff>)profit+=diff;
}
return profit;
}

3、

把交易分成两次,分别完成,最后将利润相加求最大。

public int maxProfit(int[] prices) {
if (prices.length < ) return ; int n = prices.length;
int[] preProfit = new int[n];
int[] postProfit = new int[n]; int curMin = prices[];
for (int i = ; i < n; i++) {
curMin = Math.min(curMin, prices[i]);
preProfit[i] = Math.max(preProfit[i - ], prices[i] - curMin);
} int curMax = prices[n - ];
for (int i = n - ; i >= ; i--) {
curMax = Math.max(curMax, prices[i]);
postProfit[i] = Math.max(postProfit[i + ], curMax - prices[i]);
} int maxProfit = ;
for (int i = ; i < n; i++) {
maxProfit = Math.max(maxProfit, preProfit[i] + postProfit[i]);
} return maxProfit;
}

4、

Description: 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 k transactions. Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

public int maxProfit(int k, int[] prices) {
if (prices.length < ) return ; int days = prices.length;
if (k >= days) return maxProfit2(prices); int[][] local = new int[days][k + ];
int[][] global = new int[days][k + ]; for (int i = ; i < days ; i++) {
int diff = prices[i] - prices[i - ]; for (int j = ; j <= k; j++) {
local[i][j] = Math.max(global[i - ][j - ], local[i - ][j] + diff);
global[i][j] = Math.max(global[i - ][j], local[i][j]);
}
} return global[days - ][k];
} public int maxProfit2(int[] prices) {
int maxProfit = ; for (int i = ; i < prices.length; i++) {
if (prices[i] > prices[i - ]) {
maxProfit += prices[i] - prices[i - ];
}
} return maxProfit;
}

参考:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html

可以交易k次,没看懂,感觉自己好笨。。。

Best Time to Buy and Sell Stock1,2,3,4的更多相关文章

  1. CF867E: Buy Low Sell High(贪心, STL) (hdu6438)

    Description 有nn个城市,第ii个城市商品价格为aiai​,从11城市出发依次经过这nn个城市到达n n城市,在每个城市可以把手头商品出售也可以至多买一个商品,问最大收益. Input 第 ...

  2. Lintcode393 Best Time to Buy and Sell Stock IV solution 题解

    [题目描述] Say you have an array for which the i th element is the price of a given stock on day i. Desi ...

  3. leetcode 【 Best Time to Buy and Sell Stock II 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  4. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  9. [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

随机推荐

  1. linux重启mysql无法启动

    如VPS新建后,重启则无法启动,出现类似 Starting MySQL ... * The server quit without updating PID file (/var/run/mysqld ...

  2. .net(C#)中this关键字

    使用this关键字引用成员变量使用this关键字在自身构造方法内部引用其它构造方法使用this关键字代表自身类的对象使用this关键字引用成员方法 在一个类的方法或构造方法内部,可以使用"t ...

  3. Redis系列之(二):Redis主从同步,读写分离

    1. Redis主从同步 Redis支持主从同步.数据可以从主服务器向任意数量的从服务器上同步,同步使用的是发布/订阅机制. 2. 配置主从同步 Mater Slave的模式,从Slave向Maste ...

  4. 如何有效地解决ie7,IE8不支持document.getElmentsByClassName的问题

    1.复制此代码到你js代码的最前面即可   if(!document.getElementsByClassName){ document.getElementsByClassName = functi ...

  5. JSONP跨域操作

    JSP <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3. ...

  6. [转]ExtJS学习笔记(二):handler与listener的区别

    原文地址:http://blog.csdn.net/smilingleo/article/details/3733177 ExtJS里handler和listener都是用来对用户的某些输入进行处理的 ...

  7. thinkcmf导航制作

    <?php $tree = sp_get_menu_tree('main'); ?> <foreach name="tree" item="vo&quo ...

  8. MySQL、Oracle和SQL Server的分页查询语句

    假设当前是第PageNo页,每页有PageSize条记录,现在分别用Mysql.Oracle和SQL Server分页查询student表. 1.Mysql的分页查询: SELECT * FROM s ...

  9. mysql 错误 ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number 解决办法

    MySQL创建用户(包括密码)时,会提示ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number: 问题原因: ...

  10. webApp开发

    1.Viewport:视口屏幕,可以操作的属性如下: width //viewport的宽度,范围从200-10000,默认为980像素 height //viewport的高度 initial-sc ...