当前位置:网站首页>【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
2022-07-03 07:34:00 【AQin1012】
题目描述
英文版描述
You are given an array prices where prices[i] is the price of a given stock on the i(th) day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
英文版地址
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
中文版描述
给定一个数组 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。
Constraints·提示:
1 <= prices.length <= 10(5)
0 <= prices[i] <= 10(4)
中文版地址
解题思路
需要从头遍历输入数组,分别设置两个变量(目前最小值和目前最大值),开始时将数组的第一个值同时赋给两个变量,然后从第二个开始,遇到比目前最小值小的数就重新赋值给目前最小值,遇到比目前最大值大的数就重新赋值给目前最大值,遍历结束后,返回(目前最大值-目前最小值)的差值。 每取一个值,获取他后面的值跟他之间的最大差值,返回最大的那个
解题方法
俺这版
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int minValue = prices[0];
int step = 0;
for (int i = 1; i < prices.length; i++) {
int value = (prices[i] - minValue);
if (value > 0) {
if (step < value) {
step = value;
}
} else {
minValue = prices[i];
}
}
return step;
}
}
官方版
动态规划
public class Solution {
public int maxProfit(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;
}
}
这是俺最接近官方解法的一题~~撒花*・゜゚・*:.。..。.:*・'(*゚▽゚*)'・*:.。. .。.:*・゜゚・*
边栏推荐
- URL programming
- 技术干货|昇思MindSpore NLP模型迁移之Roberta ——情感分析任务
- VMware virtual machine installation
- 你开发数据API最快多长时间?我1分钟就足够了
- An overview of IfM Engage
- Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
- IO stream system and FileReader, filewriter
- Unified handling and interception of exception exceptions of vertx
- Common operations of JSP
- 昇思MindSpore再升级,深度科学计算的极致创新
猜你喜欢
[coppeliasim4.3] C calls UR5 in the remoteapi control scenario
图像识别与检测--笔记
带你全流程,全方位的了解属于测试的软件事故
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
JS monitors empty objects and empty references
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
随机推荐
Jeecg data button permission settings
Web router of vertx
[mindspire paper presentation] summary of training skills in AAAI long tail problem
C code production YUV420 planar format file
技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
pgAdmin 4 v6.11 发布,PostgreSQL 开源图形化管理工具
How long is the fastest time you can develop data API? One minute is enough for me
lucene scorer
Wireshark software usage
Jeecg menu path display problem
Traversal in Lucene
Arduino 软串口通信 的几点体会
技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
IndexSort
URL programming
IO stream system and FileReader, filewriter
Introduction of buffer flow
The underlying mechanism of advertising on websites
TypeScript let与var的区别
[cmake] cmake link SQLite Library