当前位置:网站首页>Leetcode 0121. the best time to buy and sell stocks - simulation from back to front
Leetcode 0121. the best time to buy and sell stocks - simulation from back to front
2022-07-25 05:27:00 【Tisfy】
【LetMeFly】121. The best time to buy and sell stocks - Simulate from back to front
Force button topic link :https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/
Given an array prices , It's the first i Elements prices[i] Represents the number of shares in a given stock i Sky price .
You can only choose One day Buy this stock , And choose A different day in the future Sell the stock . Design an algorithm to calculate the maximum profit you can get .
Return the maximum profit you can make from the deal . If you can't make any profit , return 0 .
Example 1:
Input :[7,1,5,3,6,4]
Output :5
explain : In the 2 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 6) Sell when , Maximum profit = 6-1 = 5 .
Note that profit cannot be 7-1 = 6, Because the selling price needs to be higher than the buying price ; meanwhile , You can't sell stocks before you buy them .
Example 2:
Input :prices = [7,6,4,3,1] Output :0 explain : under these circumstances , No deal is done , So the biggest profit is 0.
Tips :
1 <= prices.length <= 1050 <= prices[i] <= 104
Method 1 : Simulate from back to front
Use two variables :
int M = 0; // Represents the highest stock price today and one day after
int ans = 0; // Represents maximum benefit
Go back and forth , Keep updating the maximum M M M.
Because we traverse from back to front , So if you buy stocks today , It must be able to sell at a stock price of M M M Sell when .
such , If you buy stocks today , How much money you can earn .
- Time complexity O ( N ) O(N) O(N), among N N N Is the number of days for which the stock amount is known .
- Spatial complexity O ( 1 ) O(1) O(1)
AC Code
C++
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans = 0;
int M = 0;
for (int i = prices.size() - 1; i >= 0; i--) {
M = max(M, prices[i]);
ans = max(ans, M - prices[i]); // If you buy stocks today , The biggest benefit is M - prices[i]
}
return ans;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125867999
边栏推荐
- STL notes (I): knowledge system
- 项目管理工具——项目开发者工具
- systemverilog中function和task区别
- In depth understanding of pre post + +, -- and negative modulus
- 自己实现is_class
- C Programming -- the solution of dynamic programming of "the sum of the largest subarray"
- 微信小程序相关操作示例
- Execution process of NPM run XX
- JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
- Pikachu vulnerability platform exercise
猜你喜欢

Vim查找替换及正则表达式的使用

1310_一个printf的实现分析

ThreadLocal

What should testers do if they encounter a bug that is difficult to reproduce?

The third question of force deduction -- the longest substring without repeated characters

SystemVerilog中interface(接口)介绍

自己实现is_base_of

STL notes (III): input / output stream

Panda3D keyboard moving scene

Execution process of NPM run XX
随机推荐
Performance Optimization: lazy loading of pictures
Microservice - hystrix fuse
odoo14 | 关于状态栏statusbar关键词使用后显示异常及解决方法
What is virtual DOM? How to implement a virtual DOM
obj文件格式与.mtl文件格式
学习记录[email protected]研发效能度量指标
VPP不能加载UP状态接口
1310_ Implementation analysis of a printf
The difference between $write and $display in SystemVerilog
LeetCode第302场周赛
Realsense d435i depth map optimization_ High precision mode
LeetCode 15:三数之和
LCP plug-in creates peer-to-peer 802.1ad interface
Implement is by yourself_ convertible
STL notes (I): knowledge system
epoll的实现原理
Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
CSDN编程挑战赛之数组编程问题
Use getifaddrs to obtain the IP address of the local network interface
基环树入门