博客
关于我
Leetcode 121. 买卖股票的最佳时机(DAY 26) ---- 动态规划学习期
阅读量:207 次
发布时间:2019-02-28

本文共 637 字,大约阅读时间需要 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; }};

转载地址:http://shji.baihongyu.com/

你可能感兴趣的文章
mysql 递归查找父节点_MySQL递归查询树状表的子节点、父节点具体实现
查看>>
mysql 通过查看mysql 配置参数、状态来优化你的mysql
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>
Mysql 重置自增列的开始序号
查看>>
mysql 锁机制 mvcc_Mysql性能优化-事务、锁和MVCC
查看>>
MySQL 错误
查看>>
mysql 随机数 rand使用
查看>>
MySQL 面试题汇总
查看>>
MySQL 面试,必须掌握的 8 大核心点
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>
MySQL 高性能优化规范建议
查看>>
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.6.17-win32免安装版配置
查看>>
mysql-5.7.18安装
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>