当前位置:网站首页>[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;
}
}
边栏推荐
- BS-XX-042 基于SSM实现人事管理系统
- How does win11 change icons for applications? Win11 method of changing icons for applications
- 數據訪問 - EntityFramework集成
- Jarvis OJ Telnet Protocol
- 【 brosser le titre 】 chemise culturelle de l'usine d'oies
- Jarvis OJ 简单网管协议
- 2020-2022 two-year anniversary of creation
- Detailed explanation of use scenarios and functions of polar coordinate sector diagram
- 拷贝方式之DMA
- Writing method of twig array merging
猜你喜欢

Detailed explanation of use scenarios and functions of polar coordinate sector diagram
![[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details

Etcd 构建高可用Etcd集群

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

Hiengine: comparable to the local cloud native memory database engine

Deep dive kotlin synergy (XXI): flow life cycle function
![[brush questions] effective Sudoku](/img/5b/3064170bebd1ccbee68d6a85d23830.png)
[brush questions] effective Sudoku

ECU introduction

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

调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
随机推荐
【微信小程序】一文读懂小程序的生命周期和路由跳转
Application of threshold homomorphic encryption in privacy Computing: Interpretation
干货!半监督预训练对话模型 SPACE
【剑指 Offer】61. 扑克牌中的顺子
外盘期货平台如何辨别正规安全?
composer安装报错:No composer.lock file present.
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
[61dctf]fm
Jarvis OJ 远程登录协议
JSON转MAP前后数据校验 -- 自定义UDF
Sentinel-流量防卫兵
Cs231n notes (bottom) - applicable to 0 Foundation
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
Win11提示无法安全下载软件怎么办?Win11无法安全下载软件
为季前卡牌游戏 MotoGP Ignition Champions 做好准备!
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
Domestic API management artifact used by the company
WSL2.0安装
[brush questions] effective Sudoku