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

 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. WIX、Squarespace、WordPress 三者的优劣分别是什么?

    层出不穷的智能建站,模板建站,源码建站,云建站,仿站,各种建站概念都抛洒于红海之中.到底什么样的网站适合自己,什么样的网站值得我们去消费,什么样的网站能长久,是个非常值得思考的问题. 网站建设技术非常 ...

  2. vue elementui点击表格当前行radio单选选中

    官方文档:https://element.eleme.cn/#/zh-CN/component/radio 参考:https://www.cnblogs.com/steamed-twisted-rol ...

  3. Vertex Covers(高维前缀和)

    Vertex Covers 时间限制: 5 Sec  内存限制: 128 MB提交: 5  解决: 3 题目描述 In graph theory, a vertex cover of a graph ...

  4. 关于 webpack的总结

    一 . 几个基本的概念 1. mode开发模式 // webpack.production.config.js module.exports = { mode: 'production' // 生产模 ...

  5. 本周汇总 动态rem适配移动端/块状元素居中/透明度

    1.动态rem适配移动端 !function(){ var width = document.documentElement.clientWidth; var head=document.getEle ...

  6. PHP--Button按钮没有设置type类型,默认会提交表单

    例如: <from > <input type='submit' value='提交'></input> <button >提交</button& ...

  7. python 字符串的处理技巧--join

    >>> '+'.join('1234')'1+2+3+4'>>> '+'.join(a for a in '1234')'1+2+3+4'>>> ...

  8. vue 使用 element ui动态添加表单

    html部分 <div class="hello"> <el-form :model="dynamicValidateForm" ref=&q ...

  9. 洛谷P1083 [NOIP2012提高组Day2T2]借教室

    P1083 借教室 题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借 ...

  10. jQuery的deferred对象使用详解【转载】

    一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们 ...