当前位置:网站首页>121. The Best Time to Buy and Sell Stock, the Best opportunity to Buy and Sell stocks
121. The Best Time to Buy and Sell Stock, the Best opportunity to Buy and Sell stocks
2022-08-08 02:33:00 【DXB2021】
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Given an array prices, its i element prices[i] represents a given stock i days price.
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.
You can only choose one day to buy the stock and choose to sell the stock on a different day in the future.Design an algorithm to calculate the maximum profit you can make.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Returns the maximum profit you can make on this trade.If you cannot make any profit, return 0 .
Example 1:Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (stock price = 1), buy on day 5 (stock price = 1)Sell when price = 6), maximum profit = 6-1 = 5.
Note that the profit cannot be 7-1 = 6, because the selling price needs to be greater than the buying price; at the same time, you cannot sell the stock before buying.
Example 2:Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Enter:prices = [7,6,4,3,1]Output:0Explanation: In this case, no transaction is completed, so the maximum profit is 0.
Constraints:Hint:
1 <= prices.length <= 
0 <= prices[i] <= 
C language:
int maxProfit(int* prices, int pricesSize){int diff=0;int min=prices[0];for(int i=1;iprices[i])min=prices[i];elsediff=(prices[i]-min)>diff?prices[i]-min:diff;}return diff;} Execution result: pass
Execution time: 120 ms, beat 43.28% of users in all C commits
Memory consumption: 12.6 MB, beats 62.32% of users in all C commits
Passed test case: 211 / 211
C language:
int maxProfit(int* prices, int pricesSize){int diff=0;int min=prices[0];for(int i=1;iprices[i])min=prices[i];if(prices[i]-min>diff)diff=prices[i]-min;}return diff;} Execution result: pass
Execution time: 124 ms, beat 32.14% of users in all C commits
Memory consumption: 12.5 MB, beat 89.69% of users in all C commits
Passed test case: 211 / 211
C language:
int maxProfit(int* prices, int pricesSize){int diff=0;int min=prices[0];for(int i=1;iprices[i])min=prices[i];elsediff=(prices[i]-min)>diff?prices[i]-min:diff;}return diff;} Execution result: pass
Execution time: 120 ms, beat 43.28% of users in all C commits
Memory consumption: 12.6 MB, beats 62.32% of users in all C commits
Passed test case: 211 / 211
C language:
int maxProfit(int* prices, int pricesSize){int max=0;int sum=0;for(int i=1;imax)max=sum;if(sum<0)sum=0;}return max;} Execution result: pass
Execution time: 116 ms, beat 61.56% of users in all C commits
Memory consumption: 12.8 MB, beats 15.39% of users in all C commits
Passed test case: 211 / 211
7 1 1 5 5 3 3 6 4
-6 4 4 -2 3 3 -2
4+(-2)+3=5
边栏推荐
- 海外元宇宙媒体宣发一定要关注到宣传的整体性
- 陈强教授《机器学习及R应用》课程第十一章作业
- SAP 的MPN功能
- Audio and Video Basics
- Good news | Hongshi data has obtained CMMI level 3 certification!Welcome to the self-developed unified operation and maintenance monitoring platform!
- PC博物馆(番外01)-城会玩,初中生开发实体尺规大航海游戏
- sock锁文件导致的MySQL启动失败
- Modify the jdbc data source password through the configuration file in the weblogic background
- Kubernetes技术与架构(八)
- (Note)航世BOW G19键盘 —— 使用说明书
猜你喜欢
随机推荐
深度解读 Vite 的依赖扫描?
陈强教授《机器学习及R应用》课程 第六章作业
LED驱动程序进一步优化-分层-分离
11 个不能错过的高可用技巧
程序中的负数存储及类型转换
嵌入式面试题
类和对象的深度剖析
Where to open a futures account with low fees and high returns
如何将 Tex 转化为 Word 文件
find prime problem
解决Endnote插入参考文献时导致word闪退问题
【My Diary】About my lovely new colleague
PAT甲级 1055 The World‘s Richest
The easiest way to restore classic win10 by right-clicking on Win11 (basic 0 operation)
Is it safe to buy stocks with great wisdom?Will the funds be transferred?
devops学习(十一) 构建主分支--触发器--钉钉通知
找素数问题
The futures company has all kinds of account opening fee
idea一些debug技巧
Is it safe and reliable to open a mobile stock account?









