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 (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

这道题就不仅仅只是算法题了,我们还要考虑到生活中的常识。

比如这个是Buy and Sell,就说明我们必须是先Buy然后再Sell。

比如第五天价格最低,第三天价格最高,我们肯定不能第五天买了然后穿越回去第三天卖出去啊。所以loop的时候要考虑到这些因素。

代码如下。~

public class Solution {public int maxProfit(int[] prices) {//special caseif(prices==null||prices.length<2){return 0;}int min=prices[0];int profit=0;for(int i=0;i<prices.length;i++){if(profit<(prices[i]-min)){profit=prices[i]-min;}else if(prices[i]<min){min=prices[i];}}return profit;}
}

转载于:https://www.cnblogs.com/orangeme404/p/4736289.html

[LeetCode] Best Time to Buy and Sell Stock相关推荐

  1. leetcode: Best Time to Buy and Sell Stock 系列

    leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...

  2. Leetcode Best Time to Buy and Sell Stock III

    Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...

  3. Leetcode Best Time to Buy and Sell Stock

    Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...

  4. LeetCode Best Time to Buy and Sell Stock II

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目: Say you have an array ...

  5. [LeetCode] 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 ...

  6. [leetcode]_Best Time to Buy and Sell Stock I II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  7. LeetCode Best Time to Buy and Sell Stock(dp)

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

  8. LeetCode Best Time to Buy and Sell Stock with Cooldown(动态规划)

    问题:给定一个整数数组,其中第i个元素表示第i天的股票价格.可以多次买卖股票,卖出股票后,无法在第二天买入股票. 思路:动态规划法,用dp(i,j)表示第i天持有或者不持有股票的最大利润.用dp(i, ...

  9. [leetcode]Best Time to Buy and Sell Stock III

    先说思路.参考了这篇:http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-a ...

  10. Leetcode Best Time to Buy and Sell Stock IV(最大子段和)

    朴素的O(n^2)过不了,这里借鉴前人的思想,总结一下. 主要利用栈维护对后面产生影响的字段,然后利用贪心的思想逐步合并. 首先,找到每一对最低点(vx),最高点(px); 其次,合并当前(vx, p ...

最新文章

  1. linux怎么修改grub引导顺序,我如何更改GRUB引导顺序?
  2. TLSNotary中心化预言机(2) 核心功能
  3. 扭矩大好还是马力大好_发动机的马力重要还是扭矩重要?加速到底看哪个?
  4. 使用Gardener在Google Cloud Platform上创建Kubernetes集群
  5. python列表生成多个号码_python遍历多个列表生成列表或字典
  6. 算法题目——最长连续序列
  7. 什么是eager loading
  8. ubuntu进行apt-get时候出现Package ssh is not available, but is referred to by another package 错误...
  9. model数据库orm操作
  10. Spring Boot 细节挖掘(全局异常处理)
  11. 检测对象是否拥有某一属性
  12. MATLAB中文显示乱码如何解决
  13. 问卷星图片自动提取小程序
  14. 小米线刷包需要解压么_小米10刷机教程,线刷升级更新官方系统包
  15. Idea编写简单Java网络爬虫程序(maven)
  16. qq邮箱发送邮件服务器类型,设置QQ邮箱为发送邮件服务器的详细带图步骤
  17. 基于JAVA毕业生交流学习平台计算机毕业设计源码+系统+数据库+lw文档+部署
  18. [Qt] network使用post时含有中文 reply 接收报错 bad request 错误码400
  19. rounded-{0 | top | right | bottom | left | circle } 边角半径设置 - bootStrap4常用CSS笔记(2019-05-16 09:38)...
  20. 手机快速将语音转换成文字的方法!详细步骤教你使用,方便又高效

热门文章

  1. len函数实例python_Python通过len函数返回对象长度
  2. 人脸识别-YOLOv5模型目标检测
  3. w ndows10怎么没体验指数,Win10没有“Windows体验指数”功能怎么进行系统评分【图文】...
  4. TensorFlow实践:经典CNN-AlexNet
  5. TensorFlow基础:Graph与Variable
  6. 易筋SpringBoot 2.1 | 第十七篇:SpringBoot的事务Transaction
  7. MDP马尔可夫决策过程
  8. 521. 最长特殊序列 Ⅰ
  9. 用tensorflow实现矩阵分解
  10. win10安装pyhive包