题目: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.

题目解答:给定一组数表示每天的股价,求出只交易一次时可能获得的最大利益。其实就是获得当前位置的数字与之前出现过的最小股价的差最大的时候。

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() <= 0)
            return 0;
        int min = prices[0];
        int profit = 0;
        for(vector<int>::iterator vit = prices.begin();vit != prices.end();vit++)
        {
            profit = Max (*vit - min,profit);
            if(*vit < min)
            {
                min = *vit;
            }

}
        return profit;
    }
   
    int Max(int a,int b)
    {
        return a > b ? a : b;
    }
};

转载于:https://www.cnblogs.com/CodingGirl121/p/5425087.html

Leetcode题目:Best Time to Buy and Sell Stock相关推荐

  1. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  2. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

  3. [LeetCode]122. Best Time to Buy and Sell Stock II

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

  4. 【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 ...

  5. 【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 ...

  6. 【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 ...

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

  8. 【贪心 和 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: ...

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

    LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...

  10. LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目

    此文首发于我的Jekyll博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址: ...

最新文章

  1. mysql新建库和用户linux_Linux环境 Mysql新建用户和数据库并授权
  2. linux ftp 查找文件,Linux shell ftp命令根据文件日期下载文件的方法
  3. (轉貼) C Standard Library (初級) (C/C++)
  4. jdbc和mysql编程_MySql数据库与JDBC编程三
  5. xyz转ybc_三维弯管机交互式转档软件 (UG,SOLIDWORK,PRO/E文件转成YBC)
  6. vue 函数 路由跳转_vue路由跳转的方式
  7. 傅里叶分析公式推导(最简单的傅里叶级数和傅里叶变换)
  8. java分词支持拼音_ik中文分词器及拼音分词器试用
  9. easypoi导入图片_EasyPOI—导出Excel图片问题
  10. 技术交流:对于大流量的网站,如何解决访问量的问题
  11. Maven项目设置编码
  12. 计算机制作不同数据数据图表,数据图表与分析.doc
  13. ESET最近发现了一款新的Android勒索软件,它通过向受害者的手机的联系人列表发送恶意短信继续传播
  14. TensorFlow2.0学习笔记2-tf2.0两种方式搭建神经网络
  15. Fortran 求矩阵的逆、行列式的值
  16. turtle 绘画实例1 画圆
  17. PMP-16项目整合管理-制定项目管理计划
  18. Android 是Google开发的基于Linux平台的开源手机操作系统
  19. 金仓数据库 KingbaseES SQL 语言参考手册 (11. SQL语句:ABORT 到 ALTER INDEX)
  20. “人人视频”下架整改冲上热搜;鸿蒙系统升级用户一周破千万;滴滴招股书:1300万司机去年赚了1174亿元 | 架构视点...

热门文章

  1. (0004) iOS 开发之CocoaPods 升级过程
  2. VS2010/MFC编程入门之十七(对话框:文件对话框)
  3. sql_INSERT DELETE
  4. 《C程序猿从校园到职场》勘误
  5. ml--分类与预测算法评价方法
  6. 在YII2框架中使用UEditor编辑器发布文章
  7. win7 64 安装scikit-learn
  8. CUBRID学习笔记 1 简介 cubrid教程
  9. 创业公司如何巧用工具提高团队生产力——豌豆荚创始人王俊煜讲述团队背后的“利器”...
  10. VC6.0下调bug的流程