当前位置:网站首页>【LeetCode每日一题】——121.买卖股票的最佳时机
【LeetCode每日一题】——121.买卖股票的最佳时机
2022-07-26 14:43:00 【IronmanJay】
一【题目类别】
- 数组
二【题目难度】
- 简单
三【题目编号】
- 121.买卖股票的最佳时机
四【题目描述】
- 给定一个数组 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 < = p r i c e s . l e n g t h < = 1 0 5 1 <= prices.length <= 10^5 1<=prices.length<=105
- 0 < = p r i c e s [ i ] < = 1 0 4 0 <= prices[i] <= 10^4 0<=prices[i]<=104
七【解题思路】
- 这道题可以很容易的想到两层 f o r for for循环,但是这样有的用例会超时(时间复杂度为: O ( N 2 ) O(N^2) O(N2),其中 N N N为数组长度),原因是多了很多无谓的判断,所以我们要想怎么减少这些多余的判断,可以更快的找到最大利润
- 所以我们很容易的想到了贪心算法,我们要将利润最大化,也就是说要买入的价钱最少,卖出的价钱最多,但是我们要注意要先买入,在卖出,这个可以通过示例看得更清楚
- 基于这个思路,我们维护maxProfit初始化为0,也就是利润最小是0,minPrice初始化为数组的第一个值,向后搜索时,如果遇到比minPrice还小的值就赋给minPrice表示可以以更小的价钱买入,至于哪天卖出就向后搜索即可,做减法就是利润,如果利润大于我们维护的maxProfit,就更新maxProfit
- 循环结束后返回maxProfit即可
八【时间频度】
- 时间复杂度: O ( N ) O(N) O(N),其中 N N N为数组长度
- 空间复杂度: 1 1 1
九【代码实现】
- Java语言版
package Array;
public class p121_TheBestTimeToBuyAndSellStocks {
public static void main(String[] args) {
int[] prices = {
7, 1, 5, 3, 6, 4};
int res = maxProfit(prices);
System.out.println(res);
}
public static int maxProfit(int[] prices) {
int maxProfit = 0;
int minPrice = prices[0];
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}
if ((prices[i] - minPrice) > maxProfit) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}
}
- C语言版
#include<stdio.h>
int maxProfit(int* prices, int pricesSize)
{
int maxProfit = 0;
int minPrice = prices[0];
for (int i = 0; i < pricesSize; i++)
{
if (prices[i] < minPrice)
{
minPrice = prices[i];
}
if ((prices[i] - minPrice) > maxProfit)
{
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}
/*主函数省略*/
十【提交结果】
Java语言版

C语言版

边栏推荐
- Advanced MySQL v. InnoDB data storage structure
- CVE-2022-33891 Apache spark shell 命令注入漏洞复现
- 2. Add two numbers
- 1.两数之和
- Network pictures are transferred locally, causing the kernel to exit
- LeetCode659.分割数组为连续子序列 (哈希表)
- Kubernetes ---- pod configuration resource quota
- 目标跟踪相关知识总结
- Wechat applet - "do you really understand the use of applet components?
- postman 环境变量设置代码存放
猜你喜欢

WPF 常用功能整合
![[2022 national game simulation] Bai Loujian - Sam, rollback Mo team, second offline](/img/e1/0574dd4eb311e79afdb1d071f59c4d.png)
[2022 national game simulation] Bai Loujian - Sam, rollback Mo team, second offline

基于CAS的SSO单点服务端配置

Win11 running virtual machine crashed? Solution to crash of VMware virtual machine running in win11
![[Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?](/img/5b/02a71384c62e998d40d6ce7a98082a.png)
[Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?

C# NanUI 相关功能整合

如何查找国内各大学本科学位论文?

WPF common function integration

目标跟踪相关知识总结

生泰尔科技IPO被终止:曾拟募资5.6亿 启明与济峰资本是股东
随机推荐
晶品特装递交注册:第一季营收降80% 陈波控制68.5%股权
Partial learning of PHP deserialization
CVE-2022-33891 Apache spark shell 命令注入漏洞复现
C # use shift > > and operation and & to judge whether the two binary numbers have changed
The difference between torch.nn and torch.nn.functional in pytorch
C语言入门必刷100题合集之每日一题(1-20)
【2022国赛模拟】白楼剑——SAM、回滚莫队、二次离线
VP video structured framework
精益产品开发:原则、方法与实施
VBA upload pictures
PyTorch中 torch.nn与torch.nn.functional的区别
Lean product development: principles, methods and Implementation
Tips for unity transparent channel
C# Winfrom 常用功能整合
1. Sum of two numbers
CAS single sign on
本科毕业论文外文文献翻译怎么找?
【华为联机对战服务】客户端退出重连或中途进入游戏,新玩家如何补帧?
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
Matlab solution of [analysis of variance]