Leetcode 121. 买卖股票的最佳时机(DAY 26) ---- 动态规划学习期
发布日期:2021-06-30 22:24:34 浏览次数:2 分类:技术文章

本文共 642 字,大约阅读时间需要 2 分钟。

原题题目

在这里插入图片描述



代码实现(首刷自解) 一遍遍历 但为什么效率极低

int maxProfit(int* prices, int pricesSize){
int i,min = INT_MAX,max = -1,profit = -1; for(i=0;i
max) {
max = prices[i]; if(max - min > profit) profit = max - min; } } } return profit;}

代码实现(二刷C++ 整理股票博客)

class Solution {
public: int maxProfit(vector
& prices) {
int size = prices.size(),minbuy = prices[0],maxsold = 0; for(const auto& num:prices) {
maxsold = max(maxsold,num-minbuy); minbuy = min(minbuy,num); } return maxsold; }};

转载地址:https://love6.blog.csdn.net/article/details/112802631 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Leetcode 746. 使用最小花费爬楼梯(DAY 26) ---- 动态规划学习期
下一篇:Leetcode 剑指 Offer 42. 连续子数组的最大和(DAY 25)---- 动态规划学习期

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月21日 07时44分18秒