Leetcode No.121 Best Time to Buy and Sell Stock(c++实现)
1. 题目
1.1 英文题目
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
1.2 中文题目
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
注意你不能在买入股票前卖出股票。
1.3输入输出
| 输入 | 输出 |
|---|---|
| prices = [7,1,5,3,6,4] | 5 |
| prices = [7,6,4,3,1] | 0 |
1.4 约束条件
- 1 <= prices.length <= 105
- 0 <= prices[i] <= 104
2. 实验平台
IDE:VS2019
IDE版本:16.10.1
语言:c++11
3. 分析
这一题最简单粗暴的方法就是穷举枚举,代码为:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxprofit = 0;
for (int i = 0; i < prices.size() - 1; i++)
{
for (int j = i + 1; j < prices.size(); j++)
{
int temp = prices[j] - prices[i];
if ( temp > maxprofit)
{
maxprofit = temp;
}
}
}
return maxprofit;
}
};
这样做只适用于数据较少的情形,而对于大数据很容易造成超时现象,那么能不能进行优化一下,我想到了双指针法,快指针负责遍历整个数组,当快指针的值小于等于慢指针的值时,将慢指针指向快指针指向的位置。新代码如下:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans;
for(int i = 0; i < rowIndex + 1; i++)
{
vector<int> temp(i + 1);
temp[0] = temp[i] = 1;
for(int j = 1; j < i; j++)
{
temp[j] = ans[j - 1] + ans[j];
}
ans = temp;
}
return ans;
}
};
但是我们提交后发现算法时间和空间复杂度都没变,于是我在想还有没有优化空间,我发现每行计算时都需要重新定义temp,并为其开辟内存空间,有待优化,故可以将其提前定义,并在每行计算时重定义temp大小,代码如下:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int slow = 0;
int max = 0;
int temp = 0;
for (int quick = 1; quick < prices.size(); quick++)
{
temp = prices[quick] - prices[slow];
if (temp <= 0) slow = quick;
else if (temp > max) max = temp;
}
return max;
}
};
这次性能不错。在写这段程序的过程中,我最开始是将temp在for循环内进行定义并赋值,但是我发现如果把它放在外面定义,可以优化算法的时间和空间复杂度,于是我想到,变量的定义也是需要消耗时间和空间的,因此最好是提前定义好,也就是尽量减少定义次数。
另外,在题目的solution中我还发现大佬用Kadane's Algorithm进行求解。他的大概思想就是将数组中相邻元素的差值(后减去前)重新组成一个数组,之后对这个求最大子序列和,详细介绍可以参考:https://blog.csdn.net/shendezhuti/article/details/105114569
代码如下:
class Solution {
public int maxProfit(int[] prices) {
int maxCur = 0, maxSoFar = 0;
for(int i = 1; i < prices.length; i++) {
maxCur = Math.max(0, maxCur += prices[i] - prices[i-1]);
maxSoFar = Math.max(maxCur, maxSoFar);
}
return maxSoFar;
}
};
Leetcode No.121 Best Time to Buy and Sell Stock(c++实现)的更多相关文章
- 【刷题-LeetCode】121 Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
# 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
- LeetCode OJ 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 ...
- 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- 30. leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- 安装beanstalkd队列问题——No package beanstalkd available
CentOS7.4安装beanstalkd 时无可用源 No package beanstalkd availableError:Nothing to do 可从以下获取:wget /etc/yum. ...
- Python数模笔记-PuLP库(1)线性规划入门
1.什么是线性规划 线性规划(Linear programming),在线性等式或不等式约束条件下求解线性目标函数的极值问题,常用于解决资源分配.生产调度和混合问题.例如: max fx = 2*x1 ...
- 使用原生JS,实现鼠标点击爱心效果 !!!
使用原生JS,实现鼠标点击爱心效果 !!! 引言: 在很多时候我们都需要实现鼠标点击出现图案或者文字这样的效果,对于用户而言,这样的体验是很极致的.其实实现起来也很简单,下面一起来学习一下吧.文末附上 ...
- 浅谈:Redis持久化机制(一)RDB篇
浅谈:Redis持久化机制(一)RDB篇 众所周知,redis是一款性能极高,基于内存的键值对NoSql数据库,官方显示,它的读效率可达到11万次每秒,写效率能达到8万次每秒,因为它基于内存以及存 ...
- Go语言的函数05---匿名函数
package main import ( "fmt" "time" ) //延时执行一个匿名函数 func main071() { fmt.Println(& ...
- maven项目多环境打包问题
1.xxx-api是基于springboot的模块 2.配置文件 application.properties spring.profiles.active=@activeEnv@ applicati ...
- 人脸照片自动生成游戏角色_ICCV2019论文解析
人脸照片自动生成游戏角色_ICCV2019论文解析 Face-to-Parameter Translation for Game Character Auto-Creation 论文链接: http: ...
- 向量算子优化Vector Operation Optimization
向量算子优化Vector Operation Optimization 查看MATLAB命令View MATLAB Command 示例显示Simulink编码器 ,将生成向量的块输出,设置为标量,优 ...
- postman实现参数化执行及断言处理
一.假设需要做的测试的参数如下: 注意保存为.csv文件时一定要选择格式为UTF-8 ,避免乱码. 二.输入参数和期望结果在postman中的用法: 注意一定要通过runner的方式进行运行,选择对应 ...
- 面试官:为什么Mysql中Innodb的索引结构采取B+树?
前言 如果面试官问的是,为什么Mysql中Innodb的索引结构采取B+树?这个问题时,给自己留一条后路,不要把B树喷的一文不值.因为网上有些答案是说,B树不适合做文件存储系统的索引结构.如果按照那种 ...