当前位置:网站首页>【剑指 Offer】63. 股票的最大利润
【剑指 Offer】63. 股票的最大利润
2022-07-05 16:22:00 【LuZhouShiLi】
剑指 Offer 63. 股票的最大利润
题目
假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?
思路
- dp[i]代表以prices[i]为结尾的子数组最大利润
- 状态转移方程:前i日最大利润 = max(前(i - 1)日最大利润,第i日价格 - 前i日最低价格)

代码
class Solution {
public int maxProfit(int[] prices) {
int cost = Integer.MAX_VALUE,profit = 0;
for(int price:prices){
cost = Math.min(cost,price);// 找出历史最低价格
profit = Math.max(profit,price - cost);// 更新最大利润
}
return profit;
}
}
边栏推荐
猜你喜欢

【 brosser le titre 】 chemise culturelle de l'usine d'oies

How does win11 change icons for applications? Win11 method of changing icons for applications

阈值同态加密在隐私计算中的应用:解读

美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名

Games101 notes (II)

Summary of methods for finding intersection of ordered linked list sets

If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats

How was the middle table destroyed?

中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户

如何安装mysql
随机推荐
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
【729. 我的日程安排錶 I】
Starkware: to build ZK "universe"
单商户 V4.4,初心未变,实力依旧!
【深度学习】深度学习如何影响运筹学?
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
Data access - entityframework integration
C# TCP如何限制单个客户端的访问流量
文件操作--I/O
二叉树相关OJ题
Seaborn绘制11个柱状图
sqlserver 做cdc 要对数据库性能有什么要求么
Bs-xx-042 implementation of personnel management system based on SSM
Accès aux données - intégration du cadre d'entité
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
Sentinel flow guard
npm安装
What is the difference between EDI license and ICP business license
StarkWare:欲构建ZK“宇宙”
Jarvis OJ Telnet Protocol