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. 05/08/2019 update: 其实这个题目就是[LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming的马甲题。将每个nums[i] = nums[i] - nums[i - 1], i > 1, 就是求maximum subarray。
Code
class Solution:
def bestBuySell(self, nums):
if not nums: return 0
n = len(nums)
arr, ans = [0]*n, 0
for i in range(1, n):
arr[i] = nums[i] - nums[i - 1]
mem = [0] * n
for i in range(1, n):
mem[i] = max(mem[i - 1] + arr[i], arr[i])
ans = max(ans, mem[i])
return ans
题目思路为DP, 用两个dp数组来实现, min_dp[i] 是指到i index为止的最小值, dp[i] 表示到 i index 为止能得到的最大收益
动态方程式为 min_dp[i] = min(min_dp[i-1], a[i]) , dp[i] = max(dp[i-1], a[i] - min_dp[i]), 利用滚动数组, 实现S: O(1)
init: dp[0] = 0, min_dp[0] = a[0] 1. Constraints
1) edge case: [] => 0 2. Ideas DP T: O(n) S; O(1) using rolling array 3. Code
class Solution:
def buySellStock(self, prices):
if not prices: return 0
n = len(prices)
min_dp, dp = [0]*2, [0]*2
min_dp[0], ans = prices[0], 0
for i in range(1, n):
min_dp[i%2] = min(min_dp[i%2-1], prices[i])
dp[i%2] = max(dp[i%2-1], prices[i] - min_dp[i%2])
ans = max(ans, dp[i%2])
return ans

[LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming的更多相关文章

  1. 30. leetcode 121. Best Time to Buy and Sell Stock

    121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...

  2. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

  5. Java for 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 ...

  6. leetcode 121. Best Time to Buy and Sell Stock ----- java

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

  7. Python [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 ...

  8. [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 ...

  9. Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)

    题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...

随机推荐

  1. liunx trac 邮件提示功能

    http://trac.edgewall.org/wiki/TracNotification官网上提供的方法.个人觉得不是清楚,不过还是有参考价值的.以下写下自己的添加过程,以作记录. 1.the [ ...

  2. 云计算设计模式(二十三)——Throttling节流模式

    云计算设计模式(二十三)——Throttling节流模式 控制由应用程序使用,一个单独的租户或整个服务的一个实例的资源的消耗.这种模式可以允许系统继续运行并满足服务水平协议,即使当增加需求的资源放置一 ...

  3. Android 请求运行时权限

    写文件到sd卡中,会报权限问题,需要动态申请申请运行时权限 1. MainActivity.java public class MainActivity extends Activity { priv ...

  4. vue2.0笔记《一》列表渲染

    内容中包含 base64string 图片造成字符过多,拒绝显示

  5. sencha touch datepicker/datepickerfield(时间选择控件)扩展(废弃 仅参考)

    参考资料:https://market.sencha.com/extensions/datetimepicker 上面的扩展在2.2有些问题,参考源码重新写了一个 TimePicker: Ext.de ...

  6. vue--环境搭建(创建运行项目)

    如何搭建vue环境: 1.安装之前必须要安装 node.js 2.搭建Vue环境,安装vue的脚手架工具 npm install --global vue-cli / cnpm install --g ...

  7. QrenCode : 命令行下生成二维码图片

    对于二维码大家应该并不陌生,英文名为 2-dimensional bar code 或 QR Code,是一种用图形记载信息的技术,最常见的是应用在手机应用上.用户通过手机摄像头扫描二维码或输入二维码 ...

  8. java中List的toArray方法

    把List转换成某种类型的数组,就拿String类型来做例子吧,有以下两种方式: //方法1,使用不带参数的toArray方法 String[] arr1=new String[list.size() ...

  9. 51nod 1183 - 编辑距离 - [简单DP][编辑距离问题][Levenshtein距离问题]

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183 编辑距离,又称Levenshtein距离(也叫做Edi ...

  10. linux:文件打包与压缩

    学习内容介绍:Linux 上常用的压缩/解压工具,介绍了zip.rar.tar的使用. 先总结一下常用命令: zip: 打包 :zip something.zip something (目录请加 -r ...