当前位置:网站首页>LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)
LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)
2022-08-11 03:12:00 【Know the current affairs person-HJJ】
12.买卖股票的最佳时机
给定一个数组 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
package com.leetcode;
/** * @Author Handsome * @Date 2022/8/10 17:48 * @Version 1.0 */
public class 买卖股票的最佳时机 {
public static void main(String[] args) {
System.out.println(maxProfit1(new int[]{
7, 1, 5, 3, 6, 4}));
System.out.println(maxProfit2(new int[]{
7, 1, 5, 3, 6, 4}));
// 输入 [7,1,5,3,6,4]
// 输出 5
}
/** * 暴力法: * 复杂度分析 * 时间复杂度:O(n^2).循环运行 n(n−1)/2 次. * 空间复杂度:O(1).只使用了常数个变量. */
public static int maxProfit1(int[] prices) {
int maxprofit = 0;
for (int i = 0; i < prices.length - 1; i++) {
for (int j = i + 1; j < prices.length; j++) {
int profit = prices[j] - prices[i];
if (profit > maxprofit) {
maxprofit = profit;
}
}
}
return maxprofit;
}
/** * 一次遍历: * 复杂度分析 * 时间复杂度:O(n),只需要遍历一次. * 空间复杂度:O(1),只使用了常数个变量. */
public static int maxProfit2(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}
我的学习论坛
HandsomeForum:用Java编写的学习论坛,打造我们自己的圈子!(http://huangjunjie.vip:66)
边栏推荐
- MySQL的主从复制+读写分离+分库分表,看这一篇文章就够了
- “京台高铁”亮相百度地图,真能在2035年建成吗?
- 基于FPGA状态机的自动售货机功能实现
- 对加密世界的经济误解:现金是储蓄?稀缺性创造价值?
- Summary of Logstash log data write exception troubleshooting
- IDE compilation error: Dangling metacharacter
- Typescript学习笔记 | 字节青训营笔记
- Entity到Vo的转换
- flink The object probably contains or references non serializable fields.
- leetcode:358. K 距离间隔重排字符串
猜你喜欢

Detailed explanation of new features of ES advanced function syntax

作业8.10 TFTP协议 下载功能

"How to kick a bad habit to read notes?

分布式和集群的区别和联系

Google search skills - programmer is recommended

A Practical Arrangement of Map GIS Development Matters (Part 1)

图解LeetCode——640. 求解方程(难度:中等)

DOM树的遍历-----修改样式,选择元素,创建和删除节点

Vulnhub靶机:GEMINI INC_ 2

深度学习-第二次
随机推荐
《如何戒掉坏习惯》读书笔记
①In-depth analysis of CAS SSO single sign-on framework source code
[机缘参悟-66]:怎样才能让别人愿意帮你:利益共享法则、“大道”、“人性”
IDE编译报错:Dangling metacharacter
Entity到Vo的转换
【Pdf自动生成书签】
深度学习中的模型设计
CSDN 博客更换皮肤
关于地图GIS的一次实践整理(下) Redis的GIS实践
增加对 Textbundle 的支持
Briefly, talk about the use of @Transactional in the project
leetcode:358. K 距离间隔重排字符串
(CVPR-2017)在身体和潜在部位学习深度上下文感知特征以进行行人重识别
【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
【LeetCode】Day112-repetitive DNA sequence
What does the sanction of the mixer Tornado mean for the DeFi market?
Idea (preferred) cherry-pick operation
"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
The problem that Merge will be lost again after code Revert has been solved