当前位置:网站首页>121. 买卖股票的最佳时机
121. 买卖股票的最佳时机
2022-07-21 21:57:00 【ZNineSun】
打卡!!!每日一题
今天继续给大家分享一道动态规划类型的题目。
题目描述:
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
题目示例:
可能之前有读过我打卡过的题目就知道,我们遇到动态规划类型的题目,一定不要上来就盲目的就写代码,一定要找其规律,这个规律就是解决问题的关键:状态转移方程
当然这道题,直接暴力解决,代码很好写,可是不一定能通过测试案例,我先把暴力解决的方案给出来:
public int maxProfit(int[] prices) {
int length = prices.length;
if (length <= 1) return 0;
int[] dp = new int[length];
for (int i = 0; i < length - 1; i++) {
int max = 0;
for (int j = i + 1; j < length; j++) {
int diff = prices[j] - prices[i];
max = max < diff ? diff : max;
}
dp[i] = max;
}
int max = dp[0];
for (int i = 1; i < length; i++) {
max = max < dp[i] ? dp[i] : max;
}
return max;
}
思路很简单,把每一天买入时对应的最大利润求出来,然后找最大值就好,然后看一下运行结果:
说明o(n^2)的时间复杂度不行,我们就只能乖乖的去思考其动态规划的思想了。
我们先定义
- 买入的最低点 min=prices[0],注意:min永远是最低的买入点
- dp[i]:表示在第i天所赚的差价,则我们不断的和最低点作对比
若prices[i]<min,说明最低点为price[i],更新min值为price[i],则dp[i]=0
若price[i]>=min,计算第i天的收益dp[i]=price[i]-min;
最后我们找出dp[i]中的最大值。
代码如下:
public int maxProfit(int[] prices) {
int length=prices.length;
if(length<=1)return 0;
int []dp=new int[length];
dp[0]=0;
int min=prices[0];
for(int i=1;i<length;i++){
if(prices[i]<min){
dp[i]=0;
min=prices[i];
}else{
dp[i]=prices[i]-min;
}
}
int max=dp[0];
for(int i=1;i<length;i++){
max=max<dp[i]?dp[i]:max;
}
return max;
}
不用看结果都知道这种方案要比 暴力解决效率高,最明显的就是时间复杂由o(n^2)变为o(n)
后面为继续为大家每日分享一题,也欢迎大家一起来打卡,共同进步!!!
边栏推荐
- Research on authentication scheme of distributed system
- LSA类型,OSPF的优化以及拓扑配置
- Cdh5, cdh6 Deployment Guide (stable)
- [development tutorial 5] open source Bluetooth heart rate waterproof sports Bracelet - battery power detection
- 【php环境搭建/wamp/解释器/下载】
- 缺陷报告作业
- [oops framework] local storage
- GB/T 30283-2022 信息安全技术 信息安全服务分类与代码 学习记录
- google测试框架
- JMeter advanced performance test response results are saved locally
猜你喜欢
HCIP第十二天
ECCV 2022 | 通过分析图像匹配解释深度伪造检测
What do U.S. officials think of Web3?
LSA类型,OSPF的优化以及拓扑配置
微信小程序如何实现底部弹出框封装
ECCV 2022 | interpret depth forgery detection by analyzing image matching
Installation practice of opengauss database on CentOS
打假AI无主灯,欧瑞博、艾拉物联、绿米Aqara谁是玩真的
《如何打一场数据挖掘赛事》进阶版
Pikachu character injection for Day2 POC and exp learning
随机推荐
Envoy Distributed Link Tracking
Oauth2.0 four authorization modes
How to choose sentinel vs. hystrix current limiting?
Wallpaper tutorial – beautiful underwater effects
openGauss数据库在CentOS上的安装实践
[oops framework] log management
Installation practice of opengauss database on CentOS
从基础到进阶,100道测试开发面试题,进大厂涨薪必备
网站上线功能及兼容性测试小工具集…
How to use proxy IP address
spark学习笔记(一)——模拟分布式计算
From basic to advanced, 100 test and development interview questions are necessary for entering a large factory to raise salary
Carbon fiber style plug-in navigation menu
基于JSP/SERVLET学生管理系统
黑盒测试的概念及测试方法
无代码生产新模式探索
Robust optimization of space-based relay in Internet of things based on UAV
Actual combat of flutter - customized keyboard (III)
网络安全风险评估-电信行业落地实践最佳案例
安全体系建设-安全设备监控具体操作流程