当前位置:网站首页>爬梯子&&卖卖股份的最佳时期(跑路人笔记)
爬梯子&&卖卖股份的最佳时期(跑路人笔记)
2022-06-22 04:24:00 【就一个挺垃圾的跑路人】
爬梯子

刚开始我准备使用递归来解决问题,因为这个其实就是一个斐波那契数列.
代码如下
class Solution {
public:
int climbStairs(int n)
{
if(n == 1)
return 1;
if(n == 2)
{
return 2;
}
return climbStairs(n-1)+climbStairs(n-2);
}
};
但是不行

滚动数组
所以我们就可以用现在的滚动数组来解决问题
代码如下:
class Solution {
public:
int climbStairs(int n)
{
int p = 0, q = 0, r = 1;
for (int i = 1; i <= n; ++i)
{
p = q;
q = r;
r = p + q;
}
return r;
}
};
买卖股票的最佳时机

题目意思是给你一个数组, 只能用后面的值减去前面的数组值,返回最大值即可.
暴力法
直接使用时间复杂度为O(n2)的方法代码如下.但是超时.
class Solution {
public:
int maxProfit(vector<int>& prices)
{
int ret = 0;
int tmp = 0;
for(int i = 0;i<prices.size();i++)
{
for(int j = i+1;j<prices.size();j++)
{
tmp = prices[j] - prices[i];
ret = ret>tmp?ret:tmp;
}
}
return ret;
}
};

只遍历一遍
我们先将minprices赋值给105因为prices[i]的最大值是104

class Solution {
public:
int maxProfit(vector<int>& prices)
{
int minprices = 1e5;//最大值为1*10^5^
int ret = 0;
for(int i =0;i<prices.size();i++)
{
minprices = min(minprices,prices[i]);//记录每个节点的最低值
ret = max(ret,prices[i]-minprices);//用于记录每个节点和前面最低值相减的最大值.
}
return ret;
}
};
我们这个只需要遍历一次因为我们每走过一个节点都会判断他是否比前面的值要小,如果小就记录下来.
这样后面我们在相减的时候就可以直接用当前位置减去前面的最小值而且这样的时候我们赚的是最多的.
再和之前记录的我们能赚的最大值进行比较通过一次遍历我们就可以得到我们想要的最大值了.
边栏推荐
- How do redis and MySQL maintain data consistency? Strong consistency, weak consistency, final consistency
- System V IPC and POSIX IPC
- Accumulate ERP global parameters, flexibly configure cost value rules, and complete accounting
- How does twitter decentralize? Look at these ten socialfi projects
- 图的基本概念
- [learn FPGA programming from scratch -39]: Advanced - syntax - unit test of hardware module: simulation excitation, testbench
- 音频帧大小的计算
- Popular science of source code encryption technology
- Cloud security daily 220621: Intel microcode vulnerability found in Ubuntu operating system, which needs to be upgraded as soon as possible
- Use putty to configure port mapping to realize the access of the external network to the server
猜你喜欢

Online document collaboration: a necessary efficient artifact for office

Experience and summary of embedded software testing

Interaction between C language and Lua (practice 3)

Is the first woman coming to the 3A mobile game? Recognized by IGN, can "in shining name" stand out from the encirclement

System V IPC and POSIX IPC

How to write a detailed and professional software function test report

图的BFS

Basic concept of graph

SSM inpatient management system

二叉树线索化
随机推荐
[BP regression prediction] optimize BP regression prediction based on MATLAB GA (including comparison before optimization) [including Matlab source code 1901]
Search (intensive training)
Convenient and easy to master, vivo intelligent remote control function realizes the control of household appliances in the whole house
Fastadmin list multi image preview (zoom in preview, rotation) one line of code
Existing requirements synchronize other database user information to our system. Their primary key ID is string and our primary key is long
音频帧大小的计算
DFS of graph
What is a forum virtual host? How to choose?
Shell Sort
Popular science of source code encryption technology
Customized plug-ins in Cordova project -- plug-in creation process
希尔排序
图的基本概念
Online document collaboration: a necessary efficient artifact for office
[shell] method of adding 1 to 100
Go learning notes
Internet of things UWB technology scheme, intelligent UWB precise positioning, centimeter level positioning accuracy
【shell】1加到100的方法
It is about one-step creating Yum source cache in Linux
How do redis and MySQL maintain data consistency? Strong consistency, weak consistency, final consistency