题目地址:Best Time to Buy and Sell Stock - LeetCode


Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [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.Not 7-1 = 6, as selling price needs to be larger than buying price.

Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

这道题目遍历一遍即可。
Python解法如下:

class Solution:def maxProfit(self, prices: List[int]) -> int:maxprofit=0minprice=-1for i in prices:if minprice==-1:minprice=ielif i<minprice:minprice=ielif i-minprice>maxprofit:maxprofit=i-minpricereturn maxprofit

Java解法如下:

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;}
}

C++解法如下:

class Solution {public:
int maxProfit(vector<int> &prices) {int maxPro = 0;int minPrice = INT_MAX;for(int i = 0; i < prices.size(); i++){minPrice = min(minPrice, prices[i]);maxPro = max(maxPro, prices[i] - minPrice);}return maxPro;
}
};

LeetCode 121. Best Time to Buy and Sell Stock--Java,Python,C++解法相关推荐

  1. 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock

    LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...

  2. Leetcode——121. Best Time to Buy and Sell Stock

    题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目描述 Say you have an ...

  3. leetcode 121.Best Time to Buy and Sell Stock 股票买卖最佳时间

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  4. LeetCode 121 Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  5. LeetCode 121. Best Time to Buy and Sell Stock

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  6. LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)

    翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...

  7. [LeetCode]122. Best Time to Buy and Sell Stock II

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

  8. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  9. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

最新文章

  1. 研发投入超876亿的华为,将如何进击云+AI?
  2. 比特币现金压力测试超越了24小时创造新纪录
  3. WinrRar4.2 扩展名欺骗0Day
  4. 高通fastmmi(ffbm)的使用
  5. vue生命周期大白话篇
  6. go将服务器图片响应给客户端,Go中来自客户端和服务器的RPC
  7. 推荐12款非常有用的流行 jQuery 插件
  8. 一维条形码识别c语言_条形码的优点
  9. php mongo 范围查询语句,【MongoDB】数组和范围查询的相互作用
  10. es6 方法的 name 属性
  11. java接口自动化书籍_java接口自动化优化(一)
  12. 搜索——二分搜索实现及细节
  13. php处理post序列化,使用jQuery POST和php序列化和提交表单
  14. 向数据库中的字段添加空值
  15. 图解设计模式:责任链模式
  16. 如何用U盘安装Ubuntu操作系统
  17. 智能家庭本周锋闻:小米终于还是做了空气净化器
  18. eclipse 中 svn 代码报错如下 org.apache.subversion.javahl.ClientException:Filesystem has no item
  19. 怎样旋转pdf文件页面并保存
  20. PhotoShopnbsp;CS5nbsp;官方中文正式原版下…

热门文章

  1. RDKit | 基于PCA探索化学空间
  2. WOJ 1313 - K尾相等数
  3. empty怎么发音_empty,怎么读,解答要读出来,empty怎么读慢一点,清楚一点!
  4. 如何同步更新 Github 上 Fork 的项目?
  5. 美国北卡教堂山分校Jeff Dangl组植物微生物组博士后招聘(植物微生物互作领域第一高引学者)...
  6. 斯坦福大学统计系教授带你玩转微生物组分析
  7. R语言ggplot2可视化:在可视化图像中添加对角线(diagonal line)
  8. R语言机器学习Caret包(Caret包是分类和回归训练的简称)、数据划分、数据预处理、模型构建、模型调优、模型评估、多模型对比、模型预测推理
  9. python使用statsmodels包中的robust.mad函数以及pandas的apply函数计算dataframe中所有数据列的中位数绝对偏差(MAD)
  10. python中使用squarify包可视化treemap图:使用treemap图可视化个人或者集体的股票、基金的持仓结构(treemap with squarify package)