best-time-to-buy-and-sell-stock
正常做法
超时,复杂度O(n2)O(n^2)O(n2)
class Solution {public:int maxProfit(vector<int>& prices) {int max_p = 0, curr_buy=0;auto maxPosition = max_element(prices.begin(), prices.end());if(prices.size()<1)return max_p;for(int i=0; i<prices.size() - 1; i++){if(prices[curr_buy]<prices[i])continue;maxPosition = max_element(prices.begin() + i, prices.end());int profit = *maxPosition - prices[i];if(profit > max_p)max_p = profit;curr_buy = i;}return max_p;}
};
求取一阶差分
复杂度O(n)O(n)O(n)
class Solution {public:int maxProfit(vector<int>& prices) {int sum = 0, max_sum=0;if(prices.size()<1)return max_sum;for(int i=0; i<prices.size() - 1; i++){sum += prices[i+1] - prices[i];if(sum>max_sum)max_sum = sum;if(sum<0)sum=0;}return max_sum;}
};
best-time-to-buy-and-sell-stock相关推荐
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目
此文首发于我的Jekyll博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址: ...
- LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题
此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...
- LeetCode 122. Best Time to Buy and Sell Stock II--贪心--Java,C++,Python解法
题目地址:Best Time to Buy and Sell Stock II - LeetCode Say you have an array for which the ith element i ...
- [LeetCOde][Java] Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- LeetCode Best Time to Buy and Sell Stock II
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...
- 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/ 题解 经典的 暴力递归 - ...
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
原文地址:http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html Best Time to ...
- [LeetCode] 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 ...
- [leetcode]_Best Time to Buy and Sell Stock I II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- 【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 ...
最新文章
- call_user_fun()函数的使用
- java子类有参构造函数吗_为什么我需要在这个Java通用子类中有一个构造函数?...
- stm32CAN波特率计算小程序(QT源码)
- Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案
- extent static const
- KVM虚拟化实战精讲[第五章 利用virsh对虚拟机管理]
- PyCharm的光标插入与覆盖模式
- java判断手机号_java使用正则表达式判断手机号的方法示例
- sql server中存储过程提示错误
- k8s集群部署项目_JAVA项目(部署镜像暴露应用)---K8S_Google工作笔记0062
- Spring的4种事务管理(1种编程式事务+三种声明事务)
- nginx-upload-module模块实现文件断点续传
- JupyterHub与OpenLDAP集成
- python箱形图各部分含义_箱形图以及python实践
- 使用table标签制作常用的html表格
- 柴夥說算法(4)--再說樹
- vc++实现avi文件的操作
- Scrum板与Kanban如何抉择?敏捷工具:ngoydm板与按照clwcbvam
- Description: A component required a bean of type ‘com.jia.dao.UserDao‘ that
- 东南大学研究生毕业论文LaTeX模板seuthesix的使用技巧【Mac版】
热门文章
- .net core 上传excel文件
- 解读广告SDK工作机制,保护App自身安全
- 多人协作共享画板——多人画板的bug及分析
- 用电脑看epub电子书用什么阅读软件好?
- 用Windows电脑看epub小说,哪些阅读器软件更好用?
- 修炼系列(八),你真的会写注释吗
- 使用JLINK仿真器调试树莓派4
- 音乐和计算机跨界融合,上海尝试音乐创新:中国传统文化与西方艺术跨界融合...
- 【自媒体必备】AI文字转语音,支持多种人声选择,在线生成一键导出【电脑永久版】
- 基于JAVA汽车租赁系统计算机毕业设计源码+系统+lw文档+部署