这道题想了很多,但是想多了。这个题思路很简单,如果当前值大于最小值,就计算差,和最大利润值比较。

 class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size()==)
return ;
int length = prices.size();
int minVal = prices[];
int maxP = ; for(int i=;i<length;i++)
{
if(prices[i]<minVal)
minVal = prices[i];
if(prices[i]-minVal>maxP)
maxP = prices[i]-minVal;
}
return maxP;
}
};

Best Time to Sell and Buy Stock的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LeetCode Best Time to Buy and Sell Stock with Cooldown

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: Say you hav ...

  4. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  5. Best Time to Buy and Sell Stock | & || & III

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

  6. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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

  7. 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票

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

  8. 【LeetCode OJ】Best Time to Buy and Sell Stock II

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...

  9. 309. 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 a ...

随机推荐

  1. Activiti实战04_简单流程

    在Activiti实战03_Hello World中我们介绍了一个中间没有任何任务的流程,实现了流程的部署与查阅,而在本章中,将会为流程添加任务节点,是流程能够像个流程,变得更加丰满起来. 在上一节的 ...

  2. create_pascal_tf_record.py 生成的record一直为0字节

    后面发现这个错误原来是自己Main目录下的train.txt中间没东西

  3. org.apache.ant实现压缩和解压

    <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactI ...

  4. springmvc:配置解决中文乱码的过滤器

    在web.xml中配置以下内容: <!--配置解决中文乱码过滤器--> <filter> <filter-name>characterEncodingFilter& ...

  5. 如何提高英语听力(内容摘自NECCS)+ 乘法表

    乘法表 print('\n'.join([' '.join(['%s*%s=%-2s'%(y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)]) ...

  6. LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  7. 一些CSS知识点备忘

    RGBA指的是“红色.绿色.蓝色和Alpha透明度”(Red-Green-Blue-Alpha). HSLA代表“色调.饱和度.亮度和Alpha透明度”(Hue-Saturation-Lightnes ...

  8. promise基础和进阶

    本文不对Promise的做过深的解析,只对基础的使用方法,然后会记录一些promise的使用技巧,可以巧妙的解决异步的常见问题. 在过去一直理解的是解决了一直异步回调的坑,但是用了npm async之 ...

  9. hdu oj 1520 Anniversary party(树形dp入门)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. 笔记本最小安装centos7 连接WiFi的方法

    1.首先下载iw工具. yum -y install iw 2.获取无线网卡的名称 执行iw dev,假设获得名称为 wlp3s0(示例) 3.激活无线网络接口 执行ip link set wlp3s ...