暴力法:O(n2)复杂度,即双循环,把所有的对数都比一遍,但肯定超时通过不了;

双指针递归法:左指针和右指针分别从左端和右端开始,如果prices[l]<min 那么i++ 且 min=prices[i] ;右端同理,然后做差进行比较得到result,随后递归;对 ( l+1,r ) 和 ( l , r-1 )重复计算;但仍然超时;

    void Profit(vector<int> &prices, int l, int r, int &result){if(l>=r) return;int min = prices[l], max = prices[r];if (l < r){l++;while (l < r && prices[l] < min){min = prices[l];l++;}r--;while (l < r && prices[r] > max){max = prices[r];r--;}}l--;r++;if (max > min && max - min > result)result = max - min;Profit(prices, l, r - 1, result);Profit(prices, l + 1, r, result);}int maxProfit(vector<int> &prices){int l = 0, r = prices.size() - 1;int result = 0;Profit(prices, l, r, result);return result;}

动态规划:得到评论区的一条评论,瞬间领悟;动态规划 前i天的最大收益 = max{前i-1天的最大收益,第i天的价格-前i-1天中的最小价格},这样的话O(n)的复杂度即可完成;

int maxProfit(vector<int> &prices)
{int result = 0; //前i-1天的最大收益int len = prices.size();if (len < 2)return 0;int min = prices[0];for (int i = 1; i < len; i++){if (prices[i] < min){min = prices[i];continue;}if (prices[i] - min > result)result = prices[i] - min;}return result;
}
func maxProfit(prices []int) int {len := len(prices)if len == 0 {return 0}res := 0start, end := 0, 1for ; end < len; end++ {if prices[end] < prices[end-1] {res += prices[end-1] - prices[start]start = end}}//注意别漏判了end==len的情况,此时start到end-1这一段都是递增的,应加入resif end == len {res += prices[end-1] - prices[start]}return res
}

Leetcode每日一题:121.best-time-to-buy-and-sell-stock(买股票的最佳时机)相关推荐

  1. leetcode python3 简单题121. Best Time to Buy and Sell Stock

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十一题 (1)题目 英文: Say you have an array fo ...

  2. leetcode python3 简单题122. Best Time to Buy and Sell Stock II

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十二题 (1)题目 英文: Say you have an array pr ...

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

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

  4. 【LeetCode】解题309:Best Time to Buy and Sell Stock with Cooldown(动态规划)

    LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划) Problem 309: Best Time to Buy an ...

  5. Leetcode——121. Best Time to Buy and Sell Stock

    题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目描述 Say you have an ...

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

  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

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

  9. LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)

    翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...

  10. Leetcode NO.121 Best Time To Buy And Sell Stock 买卖股票时间

    文章目录 1.问题描述 2.测试用例 示例 1 示例 2 3.提示 4.代码 1.暴力 code 复杂度 2.动态规划 code 复杂度 1.问题描述 给定一个数组 prices ,它的第 i 个元素 ...

最新文章

  1. 重磅 | 最全PPT实录!英伟达发布可编程AI推理加速器TensorRT
  2. 三十二、图的创建深度优先遍历(DFS)广度优先遍历(BFS)
  3. django基础知识~RBAC实验部分代码记录
  4. C++ COM编程之QueryInterface函数(二)
  5. EasyUI---layout布局、树形组件、选项卡tabs
  6. 量子计算机网络指数时间,科普:量子计算机是这样计算的
  7. NVIDIA新旗舰GeForce GTX 780深度评测
  8. nand flash和nor flash区别_从闪存的发展历史看,eMMC与NAND Flash有什么区别与联系?...
  9. mac如何删除用户或者群组
  10. 工具使用-----Jmeter-脚本的录制
  11. NI 国家仪器 各版本软件下载链接
  12. 国家测绘局制定的测绘与地理信息行业标准统计 (20110706)
  13. lua table的__index ,setmetable()的一些操作
  14. Springboot数据库配置文件明文密码加密解密
  15. matlab图无线型,如何使用MATLAB进行移动无线信道模型的建模资料概述
  16. HTML音频:音乐播放网页
  17. python Deformation Transfer for Triangle Meshes
  18. 如何让Citrix客户端不能访问XenApp服务器的磁盘
  19. Java随笔-String有多长?
  20. onenote标注pdf笔记_GoodNotes 5 手写笔记和PDF标注必备工具

热门文章

  1. java list集合运算
  2. 一款APP设计的从0到1之:Android设计规范篇(转载)
  3. Groovy获取json和xml数据
  4. Java经典23结构模型的设计模式(三)------附加代理模式、适配器型号、Facade模式的差异...
  5. 一般处理程序页ashx 序列化 Json数组
  6. bianma 水平 技巧
  7. IBatisNet的配置(SqlMap.config)
  8. 20 ide配置快捷键补全提示 win
  9. JavaScript基础知识(三个判断、三个循环)
  10. WebApi实现验证授权Token,WebApi生成文档等