当前位置:网站首页>【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;
}
}
这是俺最接近官方解法的一题~~撒花*・゜゚・*:.。..。.:*・'(*゚▽゚*)'・*:.。. .。.:*・゜゚・*
边栏推荐
- Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
- The difference between typescript let and VaR
- Beginners use Minio
- Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
- Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
- Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
- 论文学习——鄱阳湖星子站水位时间序列相似度研究
- Logging log configuration of vertx
- Vertx metric Prometheus monitoring indicators
- 不出网上线CS的各种姿势
猜你喜欢
Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
【CoppeliaSim4.3】C#调用 remoteApi控制场景中UR5
VMware network mode - bridge, host only, NAT network
Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
Leetcode 213: looting II
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
c语言指针的概念
Topic | synchronous asynchronous
随机推荐
Beginners use Minio
Jeecg data button permission settings
Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
IO stream system and FileReader, filewriter
PgSQL converts string to double type (to_number())
Unified handling and interception of exception exceptions of vertx
技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验
C code production YUV420 planar format file
论文学习——鄱阳湖星子站水位时间序列相似度研究
圖像識別與檢測--筆記
gstreamer ffmpeg avdec解码数据流向分析
最全SQL与NoSQL优缺点对比
Pgadmin 4 v6.11 release, PostgreSQL open source graphical management tool
Comparison of advantages and disadvantages between most complete SQL and NoSQL
sharepoint 2007 versions
Vertx metric Prometheus monitoring indicators
Implementation of breadth first in aggregation in ES
Inverted chain disk storage in Lucene (pfordelta)
你开发数据API最快多长时间?我1分钟就足够了