当前位置:网站首页>买卖股票的最佳时机
买卖股票的最佳时机
2022-06-28 14:51:00 【华为云】
title: 买卖股票的最佳时机
date: 2022-04-24 13:35:24
tags: 每天进步一点点系列
题目
难度 简单
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
示例 1:
输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。示例 2:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
提示:1 <= prices.length <= 105
0 <= prices[i] <= 104
代码:
class Solution { public int maxProfit(int[] prices) { //dp dp[i]表示该天的最大利益 int[] dp = new int[prices.length]; //最大利益 int max = 0; //从第二天开始,每天的最大利益取决于前一天的最大利益和当天的价格 for (int i = 1; i < prices.length; i++) { //当前天的最大利益 = 前一天的最大利益 + 当天的价格 - 前一天的价格 //如果当天的利益小于0,则当天的利益为0 dp[i] = Math.max(dp[i-1] + prices[i] - prices[i-1],0); //更新最大利益 max = Math.max(max,dp[i]); } //返回最大利益 return max; }}每日单词

以上就是买卖股票的最佳时机的全部内容
版权声明:
原创博主:牛哄哄的柯南
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的==一键三连==支持!
[哈哈][抱拳]

加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- 请问一下,是不是insert all这种oracle的批量新增没拦截?
- [spatial & single cellomics] phase 1: Study on PDAC tumor microenvironment by single cell binding spatial transcriptome
- Force deduction solution summary 522- longest special sequence II
- Could you tell me whether the batch addition of Oracle such as insert all was not blocked?
- Technical trendsetter
- Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index
- Ionq and Ge research confirmed that quantum computing has great potential in risk aggregation
- code snippet
- 2022 welder (technician) examination question bank simulated examination platform operation
- Recommended practice sharing of Zhilian recruitment based on Nebula graph
猜你喜欢

Angers medical sprint scientific innovation board: annual revenue of RMB 300million and proposed fund raising of RMB 770million

蔚来潜藏的危机:过去、现在到未来

Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion

Talking from the little nematode -- tracing the evolution of nervous system and starting life simulation

Steve Jobs of the United States, died; China jobs, sold

腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元

动力电池,是这样被“瓜分”的

WPF 视频硬解码渲染播放(无空域)(支持4K、8K、高帧率视频)

PMP认证证书的续证费用是多少?

美因基因港交所上市:市值43亿港元 IPO被市场忽略
随机推荐
抽奖动画 - 鲤鱼跳龙门
Leetcode(406)——根据身高重建队列
Maingene listed on the Hong Kong Stock Exchange: IPO with a market value of HK $4.3 billion was ignored by the market
How does Seata server 1.5.0 support mysql8.0?
324. 摆动排序 II : 不简单的构造题
Opening and closing principle
[MySQL learning notes 24] index design principles
法兰克福地区目前支持sql了吗?
Which is safer, a securities company or a bank? How to open an account is the safest
蔚来潜藏的危机:过去、现在到未来
WSUS客户端访问服务端异常报错-0x8024401f「建议收藏」
Softing epGate PB系列网关-可将PROFIBUS总线集成到EtherNet/IP网络
[C language] implementation of binary tree and three Traversals
Functools: high order functions and operations on callable objects (continuous updating ing...)
Leetcode (88) -- merge two ordered arrays
Combined sum leetcode
Configuration file encryption (simple use of jasypt)
Based on asp Net based document retrieval system
Four visualization tools are recommended to solve 99% of large screen visualization projects!
Le patron a donné trois ordres: discret, discret, discret