题目原址

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/

题目描述

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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example2:

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

解题思路

翻译:
你有一个数组,数组中的数是当天的股票价格,如prices[1] = 10,表示第2天的股票价格为10
你最多有一次交易的机会(即:只能买一次股票,卖一次股票)设计一个算法找出交易的最大利润。

  • 这道题使用Kadane’s algorithm(Kadane算法)实现,Kadane是卡内基梅隆大学的教授,而该算法是为了解决最大子序列的和(maximum subarray)提出的。
  • Leetcode上面有很多求最大子序列和的问题,这种题应该算是一个类型的。
  • 解决该题的方法还是很简单的:
    • 判断当当前最大利益 + 当前的元素-当前元素的前一个元素的值是否是负数,如果是负数,就说明已经赔没了,不能买当前股票的前面的股票了,所以当前的利益为0。如果值为正数,则还可以往下遍历。
    • 每次更改当前利益之后,都要判断当前利益与总利益之间的大小,保证总利益永远最大。

AC代码

class Solution {public int maxProfit(int[] prices) {int max_ending_here = 0;int max_so_far = 0;for(int i = 1; i < prices.length; i++) {max_ending_here = Math.max(0, prices[i] - prices[i-1] + max_ending_here);max_so_far = Math.max(max_so_far, max_ending_here);}return max_so_far;   }
}

感谢

https://discuss.leetcode.com/topic/19853/kadane-s-algorithm-since-no-one-has-mentioned-about-this-so-far-in-case-if-interviewer-twists-the-input/3

Leetcode——121. Best Time to Buy and Sell Stock相关推荐

  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 股票买卖最佳时间

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

  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 were ...

  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 w ...

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

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

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

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

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

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

  8. 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/ 题解 经典的 暴力递归 - ...

  9. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

最新文章

  1. Visual Studio 2012 调试程序加载缓慢,提示正在下载符号
  2. 下载CentOS7、CentOS8镜像(ISO)
  3. 横向ListView(四) —— 添加滚动条
  4. Go中new和make的区别
  5. c语言判断字符串是不是回文_LeetCode 热题 HOT 100 5. 最长回文子串
  6. 设计模式系列 - 原型模式
  7. h5 换脸 php,【部分原创】python实现视频内的face swap(换脸)
  8. 学生信息管理---C#文件写入及读取
  9. 获得国内中国电信,网通,铁通的最新ip段的方法
  10. 如果/否则列表理解?
  11. js的5种继承方式——前端面试
  12. iOS:使用block进行类与类,控制器与控制器之间的传值
  13. Kicad快捷键大全
  14. Springboot毕设项目餐厅后勤管理系统cxjgl(java+VUE+Mybatis+Maven+Mysql)
  15. 5G无线系统设计与国际标准 pdf和Word
  16. centos7桌面没有计算机图标,Centos7,桌面图标消失且右键失效
  17. 通过linux进程号(pid)查找Oracle的session中都执行那些sql
  18. ros机器人gazebo仿真
  19. 旋转矩阵、欧拉角、四元数理论及其转换关系
  20. F1赛车相关公开数据集

热门文章

  1. 基于FPGA的电子计算器设计(上)
  2. 烘焙门店 | 解锁公众号精准引流5W+的流量密码
  3. 通过展频降低系统EMI
  4. ​力扣解法汇总764. 最大加号标志
  5. java B2B2C Springcloud电子商务平台源码
  6. MLDLRL:MLDL相关概念的原始英文解释——对理解最初的ML和DL的相关概念的定义非常有用
  7. android gravity参数,android - 如何以编程方式设置layout_gravity?
  8. 树莓派与Arduino串口通讯获取DH11温湿度
  9. Flink使用JdbcSink下沉数据到数据库MySQL
  10. 虹科ELPRO的智能数据记录仪由Sensirion技术驱动