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

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.


题解:简单的动态规划题。用prices存储每天的股票价格,dp[i]表示第i天的最大利润,minmum表示遍历到当前为止prices中最小的元素。

那么在第i+1天有两种选择:

1.在这一天卖掉股票,那么显然可以获得的最大利润是prices[i+1]-minmum;

2.在这一天之前就把股票卖掉,那么可以获得的最大利润即使前i天可以获得最大利润。

dp[i] = max({dp[0],dp[1],...,dp[i]},prices[i+1]-minmum)

其实上述的dp数组也不需要,只要一个profit变量记录到当前为止可以获得最大的利润就可以了。

代码如下:

#include <iostream>
#include <vector>
using namespace std;class Solution {
public:int maxProfit(vector<int> &prices) {if(prices.size() == 0)return 0;int Profit = 0;int minmum_price = prices[0];for(int i= 1;i < prices.size();i++){if(prices[i] < minmum_price)minmum_price = prices[i];if(prices[i]-minmum_price > Profit)Profit = prices[i]-minmum_price;}return Profit;}
};int main(){vector <int> prices;prices.push_back(3);prices.push_back(3);prices.push_back(5);prices.push_back(0);prices.push_back(0);prices.push_back(3);prices.push_back(1);prices.push_back(4);Solution s;cout <<s.maxProfit(prices)<<endl;
}

JAVA版本代码:设置一个最小值记录当前为止最低的价格,一个answer变量记录当前为止可以获得最大收益,那么在每一步answer = max(answer,prices[i]-nowMin),最后的answer就是最大的收益。

 1 public class Solution {
 2     public int maxProfit(int[] prices) {
 3         int nowMin = Integer.MAX_VALUE;
 4         int answer = 0;
 5         for(int i = 0;i < prices.length;i++){
 6             nowMin = nowMin < prices[i]?nowMin:prices[i];
 7             answer = answer > prices[i]-nowMin?answer:prices[i]-nowMin;
 8         }
 9         return answer;
10     }
11 }

转载于:https://www.cnblogs.com/sunshineatnoon/p/3732428.html

【leetcode】Best Time to Buy and Sell Stock相关推荐

  1. 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

    原文地址:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html Best Time to ...

  2. 【CODE】Best Time to Buy and Sell Stock

    买卖股票问题:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/yi-ge-tong-yong ...

  3. 【leetcode】best time to buy and sell stocks(i, ii, iii, iv, v)

    i.只允许一次交易 思路:从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求. class Soluti ...

  4. leetcode【121】Best Time to Buy and Sell Stock【c++,O(n)复杂度,时间97%,空间100%】

    问题描述: Say you have an array for which the ith element is the price of a given stock on day i. If you ...

  5. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

  6. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

  7. 【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV

    LeetCode 188. Best Time to Buy and Sell Stock IV Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyan ...

  8. 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III

    LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...

  9. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II

    LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...

最新文章

  1. python如何调用c++numpy.ndarray代码_python – 在cython中使用numpy:定义ndarray数据类型/ ndims...
  2. ifconfig命令实例
  3. 多元价值呼唤教育性父母
  4. Web前端开发怎样学?你需了解这些问题
  5. linux除了cat读取文件内容,linux cut命令和cat命令以及查看文件内容命令总结
  6. Facebook应用Moments使用C++实现跨平台代码共享
  7. 通过ceph-deploy搭建ceph 13.2.5 mimic
  8. mysql数据库下的所有表字段
  9. 【POJ1723】SOLDIERS(中位数,货仓选址)
  10. 疫情之下,你有多久没回过家了?
  11. 中职学校计算机教学中,探析中职学校计算机教学中的一些体会和思考.doc
  12. java 自然常数e中出现的连续的第一个10个数字组成的质数_自然常数-常数e的来历e在很多数学公式中出现的频率比较高今天做导数题时看到 爱问知识人...
  13. idea java maven 打包,idea maven项目 基于idea自己打包方式 以及使用maven插件打包的三种方式...
  14. c# .net PayPal支付验证
  15. android 10 长按Power键跳过关机对话框直接关机
  16. 人类一败涂地做图教程_人类一败涂地皮肤怎么弄 人类一败涂地皮肤制作教程...
  17. 微信小程序————简易计算器
  18. window统计连接数
  19. 21个免费的UI界面设计工具、资源及网站
  20. 拉格朗日乘数法及python实现

热门文章

  1. 商丘网络推广浅析如何分析竞争对手的网站,制定更优秀的优化方案?
  2. 网络营销专员浅析在网络营销推广中有不少渠道可适当“利用”一下
  3. 一般物流网站建设有哪些必备版块?
  4. 如何查看笔记本电脑配置参数_2020双十一(小白/学生)如何选购笔记本电脑?5000元预算哪款笔记本配置值得入手?...
  5. 虚拟化服务器端口用万兆,万兆以太网部署需要注意的方面有哪些
  6. ixf文件 mysql导入,控制 PC/IXF 文件导入数据库的一般规则
  7. 钉钉获取免登授权码 php_php代码如何加域名授权?开源php项目如何保护版权 商业授权?...
  8. 微信小程序从oracle取数,微信小程序 取随机数
  9. 深度学习实战篇-基于RNN的中文分词探索
  10. go语言笔记——defer作用DB资源等free或实现调试