当前位置:网站首页>[Jianzhi offer] 63 Maximum profit of stock
[Jianzhi offer] 63 Maximum profit of stock
2022-07-05 16:58:00 【LuZhouShiLi】
The finger of the sword Offer 63. The biggest profit of stocks
subject
Suppose you store the price of a stock in an array in chronological order , What's the maximum profit you can get from buying and selling this stock at one time ?
Ideas
- dp[i] Representative to prices[i] Maximum profit for the ending subarray
- State transition equation : front i Maximum daily profit = max( front (i - 1) Maximum daily profit , The first i Daily price - front i Daily minimum price )

Code
class Solution {
public int maxProfit(int[] prices) {
int cost = Integer.MAX_VALUE,profit = 0;
for(int price:prices){
cost = Math.min(cost,price);// Find the lowest price in history
profit = Math.max(profit,price - cost);// Update maximum profit
}
return profit;
}
}
边栏推荐
- 【刷题篇】有效的数独
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
- 拷贝方式之DMA
- 网站页面禁止复制内容 JS代码
- Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
- Etcd build a highly available etcd cluster
- Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
- Etcd 构建高可用Etcd集群
- 干货!半监督预训练对话模型 SPACE
- 【刷題篇】鹅廠文化衫問題
猜你喜欢
![[61dctf]fm](/img/22/3e4e3f1679a27d8b905684bb709905.png)
[61dctf]fm

Deep learning plus

数据访问 - EntityFramework集成

Hiengine: comparable to the local cloud native memory database engine

How to uninstall MySQL cleanly

Desci: is decentralized science the new trend of Web3.0?

精准防疫有“利器”| 芯讯通助力数字哨兵护航复市

普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理

Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)

Jarvis OJ 远程登录协议
随机推荐
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
C# TCP如何设置心跳数据包,才显得优雅呢?
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Copy mode DMA
How to uninstall MySQL cleanly
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
Cs231n notes (bottom) - applicable to 0 Foundation
Domestic API management artifact used by the company
二叉树相关OJ题
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
【729. 我的日程安排錶 I】
Win11如何给应用换图标?Win11给应用换图标的方法
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
[js] 技巧 简化if 判空
SQL injection of cisp-pte (Application of secondary injection)
【微信小程序】一文读懂小程序的生命周期和路由跳转
网站页面禁止复制内容 JS代码
浏览器渲染原理以及重排与重绘
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
Combined use of vant popup+ other components and pit avoidance Guide