首页 > 其他分享 >[LeetCode][121]best-time-to-buy-and-sell-stock

[LeetCode][121]best-time-to-buy-and-sell-stock

时间:2023-08-23 15:24:24浏览次数:30  
标签:sell buy profit 121 prices day stock

Content

You are given an array prices where prices[i] is the price of a given stock on the ith 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.

 

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.

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

 

Constraints:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104
Related Topics
  • 数组
  • 动态规划

  • 标签:sell,buy,profit,121,prices,day,stock
    From: https://www.cnblogs.com/shea24/p/17651730.html
  • 相关文章

    • AOJ0121(Seven Puzzle, BFS+Cantor+逆向思维)
      参考康托展开自己真的一点头绪都没有,根据前面的大神博客自己几乎100%复制了一个,但是还是WA,觉得还是出在选方向时的判断上面。Cantor感觉这个可以作为模板,状态压缩的一个思路。还有就是BFS特点:1.从一个起点到一个终点2.起点和终点可以互相到达,双向的//#defineLOCAL#include......
    • LeetCode 121.买卖股票的最佳时机
      1.题目:给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返回你可以从这笔交易中获取的最大利润。如果你不能获......
    • 做题记录:P3121 [USACO15FEB] Censoring G
      题目传送门:clickhere题意简化:给定一个文本串,和n个匹配串,删掉文本串中的匹配串求最后的字符串做这题之前应该先做简化版:eazymode上面这题用kmp+栈就能过以前如果用的是\(erase\)函数是错解,字符串的\(erase\)时间复杂度是常数级别的看到这道题后非常的高兴,直接打了个爆力跳......
    • P1217 [USACO1.5] 回文质数 Prime Palindromes
        打表先把一到一亿的质数兼回文数打出来。(用文件输入输出会方便复制一些)最后效果如下:太长故折叠 0,2,3,5,7,11,101,131,151,181,191,313,353,373,383,727,757,787,797,919,929,10301,10501,10601,11311,11411,12421,12721,12821,13331,13831,13931,14341,14741,15451,1......
    • 121. 买卖股票的最佳时机
      给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返回你可以从这笔交易中获取的最大利润。如果你不能获取任何......
    • day121 - 依赖注入的几种方式(2)
      依赖注入的几种方式为数组类型属性赋值直接配置property中的array属性<beanid="studentFive"class="com.gu.spring.pojo.Student"><propertyname="sid"value="1003"></property><propertyname="sname"value......
    • UVA 12170
      从另一个网站上的我的博客里转的。感觉放在一起比较好。时间久远,而且是英文(流泪)。EasyClimbStep1If\(x_i,d\le100\).Thendefine\(dp_{i,j}\)astheminimumcostforthefirst\(i\)stacks,with\(j\)astheheightofthe\(i^{\tt{th}}\)stack.Then,theformu......
    • POJ - Buy Tickets
      Smiling&Weeping----你看这个人,嘴里说着喜欢我却又让我这么难过DescriptionRailwayticketsweredifficulttobuyaroundtheLunarNewYearinChina,sowemustgetupearly......
    • P1219 八皇后 Checker Challenge(深度搜索dfs经典问题+回溯)
      题目连接:P1219[USACO1.5]八皇后CheckerChallenge-洛谷|计算机科学教育新生态(luogu.com.cn) 典型的深度优先搜索的问题----》先付代码再来跟新java组代码packagePTACZW;importjava.util.Scanner;importjava.io.*;importjava.util.Set;importjava.util.Has......
    • 1212
       窗体顶端 号牌种类 小型汽车号牌号码 PH1W19机动车状态 正常所有人 李金涛抵押标记 ****住所地址 山东省临清市先锋街道郭堤居******登记证书编号 370010902690联系地址 山东省临清市先锋街道郭......