题目原址

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. 完全平方数(打表+二分)
  2. Android onclicklistener中使用外部类变量时为什么需要final修饰【转】
  3. 树莓派网页服务器的网页留言板,树莓派利用Django搭建聊天网页服务器 —— 准备篇...
  4. linux磁盘权限 /srv,Linux学习笔记之解压压缩,磁盘分区,软件包管理,权限
  5. css li 空隙问题
  6. 计算机组装与维护时dm是指,《计算机组装与维护》试题答案
  7. NIS认证管理域中的用户
  8. h5侠客行服务器维护有更新什么,《侠客行》1月22日版本更新说明
  9. matlab保存m文件是什么意思,在Matlab中怎样把一个程序保存为一个.m文件
  10. Rust的错误处理机制
  11. (14)写一个函数,将两个字符串连接
  12. Unity_7 如何使用遮挡剔除Occlusion Culling
  13. 今日头条开通,分享我爱的数码科技
  14. 二分答案(by jie)
  15. 【Fabric】- Fabric官网案例First-network
  16. 《创新思维设计》自学报告#2 | 设计思维的特征
  17. Http chunk介绍
  18. 【网络流】【二分图最大匹配】Buaacoding1043 难题·Beihang Couple Pairing Comunity 2017
  19. java linux mdb,在Linux下实现对Microsoft Access Database(.mdb)查询访问
  20. Docker学习之路(三)Docker网络详解

热门文章

  1. 基于DS18B20数字温度传感器的温度计设计
  2. manjaro 安装搜狗输入法不显示候选词
  3. java selector 源码_Java NIO——Selector机制源码分析---转
  4. java开发中常用的算法_总结一下项目开发过程中常用的到的一些加密算法。
  5. excel自学第1天_excel制作项目时间进度表_excel表头
  6. VC驿站黑客编程(关机,重新启动,注销)
  7. 【Codeforces】764A Compote
  8. 静态存储器(SRAM)工作原理
  9. python最适合做什么生意好-本周互联网关注(2015515):劳动人民的生意经、python好还是go好...
  10. 实战十四:基于线性回归预测环境空气质量 代码+数据