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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.

分析

首先通过把相邻的股票价格相减得到一组差价,后面就转化成了最大子列和问题了;另外注意当传进来的prices的数组为空时,返回0;

class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()==0) return 0;
vector<int> profits(prices.size());
profits[0]=0;
for(int i=1;i<profits.size();i++)
profits[i]=prices[i]-prices[i-1];
int maxsum[profits.size()]={0},maxprofit=0;
for(int i=1;i<profits.size();i++){
maxsum[i]=max(profits[i],maxsum[i-1]+profits[i]);
maxprofit=max(maxprofit,maxsum[i]);
}
return maxprofit;
}
};

121. Best Time to Buy and Sell Stock(动态规划)的更多相关文章

  1. Leetcode 121 Best Time to Buy and Sell Stock 动态规划

    由于题意太长,请自己翻译,很容易懂的. 做法:从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求.动态规 ...

  2. 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  3. 30. leetcode 121. Best Time to Buy and Sell Stock

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  4. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  5. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  6. 121. Best Time to Buy and Sell Stock@python

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

  7. [LeetCode] 121. 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 ...

  8. 【刷题-LeetCode】121 Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  9. 121. 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 ...

随机推荐

  1. 继承自TWinControl的控件不能在设计期间接受子控件,用代码设置子控件却可以(它的自绘是直接改写PaintWindow虚函数,而不是覆盖Paint函数——对TWinControl.WMPaint又有新解了)

    这个控件直接继承自TWinControl,因此不是改写Paint;函数,而是直接改写PaintWindow虚函数,它在VCL框架里被直接调用,直接就把自己画好了(不用走给控件Perform(WM_Pa ...

  2. 乐视云监控数据存放到influxdb中

    3.9     监控.告警系统 监控报警我们分PaaS平台和业务应用两大类. PaaS平台主要聚焦在基础设施和LeEngine的各个服务组件的监控报警(比如主机CPU,内存,IO,磁盘空间,LeEng ...

  3. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp

    这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...

  4. bzoj3527

    http://www.lydsy.com/JudgeOnline/problem.php?id=3527 今天肿么这么颓废啊...心态崩了 首先我们得出Ei=Fi/qj,然后我们设f[i]=1/i/i ...

  5. Spark 机器学习 ---Word2Vec

    package Spark_MLlib import org.apache.spark.ml.feature.Word2Vec import org.apache.spark.sql.SparkSes ...

  6. E20170925-hm

    arc  n. 综合症状; 弧(度); 天穹; 电弧,弧光.; vi. 形成拱状物; 循弧线行进; wrap  vt. 包; 缠绕; 用…包裹(或包扎.覆盖等); 掩护;            n. ...

  7. [Swift通天遁地]六、智能布局-(7)通过Group(组)命令实现对多个视图的统一约束

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. 《Akka应用模式:分布式应用程序设计实践指南》读书笔记9

    性能 这也是一个比较大的问题,因为性能不一定是Akka本身的问题,还可能是你代码写的有问题. 优化的第一步就是找出性能的瓶颈,隔离出应用程序里面比较耗时的部分,然后尝试对其优化,减少需要耗费的时间成本 ...

  9. Java经典算法之插入排序(Insert Sort)

    插入排序在局部有序的情况下比冒泡排序快一倍,比选择排序快一点. 那什么是插入排序,就是将局部有序的数据向右移动,将未排序的数据插到他的前面 下面我们来解析代码: 这里外层循环out变量从1开始向右移动 ...

  10. Scaffold-DbContext-EFCore DB First

    在使用 Scaffold-DbContext迁移数据库的时候会遇到两个问题. 一.文件已存在,其实报错很明显,增加 -force即可. 二.大小写转换,不和数据库一样了,如果要保持和数据库一致.增加  ...