当前位置:网站首页>The best time to climb a ladder & sell shares (notes of the runner)
The best time to climb a ladder & sell shares (notes of the runner)
2022-06-22 04:26:00 【Just a garbage runner】
Climbing a ladder
70. climb stairs - Power button (LeetCode)

At first I was going to use recursion to solve the problem , Because this is actually a Fibonacci sequence .
The code is as follows
class Solution {
public:
int climbStairs(int n)
{
if(n == 1)
return 1;
if(n == 2)
{
return 2;
}
return climbStairs(n-1)+climbStairs(n-2);
}
};
But I can't

Scrolling array
So we can use the current rolling array to solve the problem
The code is as follows :
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;
}
};
The best time to buy and sell stocks
The best time to buy and sell stocks - Power button (LeetCode)

The title means to give you an array , You can only subtract the previous array value from the latter value , Return the maximum value .
Violence law
The direct use time complexity is O(n2) The method code of is as follows . But timeout .
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;
}
};

Just go through
We will first minprices Assign a value to 105 because prices[i] The maximum value of 104

class Solution {
public:
int maxProfit(vector<int>& prices)
{
int minprices = 1e5;// The maximum value is 1*10^5^
int ret = 0;
for(int i =0;i<prices.size();i++)
{
minprices = min(minprices,prices[i]);// Record the minimum value of each node
ret = max(ret,prices[i]-minprices);// Used to record the maximum value of each node minus the previous lowest value .
}
return ret;
}
};
We only need to traverse this node once, because every time we walk through a node, we will judge whether it is smaller than the previous value , If you are young, write it down .
In this way, we can directly subtract the previous minimum value from the current position when subtracting, and we earn the most in this case .
Then compare with the maximum value we can earn recorded before, and we can get the maximum value we want through a traverse .
边栏推荐
- Tianyang technology - Bank of Ningbo interview question [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]
- Write the first C application -- Hello, C
- active RM机子断电后,RM HA切换正常。但是YarnUI上查看不到集群资源,application也一直处于ACCEPTED状态。
- KS004 基于SSH通讯录系统设计与实现
- yum命令
- It is about one-step creating Yum source cache in Linux
- Popular science of source code encryption technology
- Clue binary tree
- Is it safe to open an account in Guoyuan futures?
- Es cannot work, circuitbreakingexception
猜你喜欢

Use of markdown markup language

Introduction to AWS elastic Beanstalk

About SSM integration, this is enough ~ (nanny level hands-on tutorial)

快速排序

Spark - Executor 初始化 && 报警都进行1次

插入排序

Storage structure of tree

Multithread interrupt usage

Use putty to configure port mapping to realize the access of the external network to the server

Internet of things UWB technology scheme, intelligent UWB precise positioning, centimeter level positioning accuracy
随机推荐
It is about one-step creating Yum source cache in Linux
哈夫曼树
FaceShifter. ipynb
Interaction between C language and Lua (practice 3)
Accumulate ERP global parameters, flexibly configure cost value rules, and complete accounting
Wechat applet uploading seven cattle cloud laravel
FaceShifter.ipynb
Pytorch之contiguous函数
New chief maintenance personnel for QT project
Daily question: the difference between ArrayList and LinkedList
Storage structure of tree
Tencent side
Laravel implements soft deletion
BFs of figure
Low power radar sensing module, application of smart lock radar sensing scheme, smart radar sensor technology
套用这套模板,玩转周报、月报、年报更省事
Tencent一面
低功耗雷达感应模组,智能锁雷达感应方案应用,智能雷达传感器技术
Windows10 cannot access LAN shared folder
window10无法访问局域网共享文件夹