[LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

时间:2022-10-23 00:22:03

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. Oracle数据库迁移到AWS云的方案

    当前云已经成为常态,越来越多的企业希望使用云来增加基础设施的弹性.减轻基础设施的维护压力,运维的成本等.很多企业使用云碰到的难题之一是如何将现有的应用迁移到云上,将现有应用的中间件系统.Web系统及其 ...

  2. silverlight MouseLeftButtonDown事件总是无法触发

    参考解决办法:http://www.cnblogs.com/tianguook/archive/2011/05/13/2045299.html 在构造函数中首先添加一个事件: public BtnLi ...

  3. spring beans源码解读之 ioc容器之始祖--DefaultListableBeanFactory

    spring Ioc容器的实现,从根源上是beanfactory,但真正可以作为一个可以独立使用的ioc容器还是DefaultListableBeanFactory,因此可以这么说, DefaultL ...

  4. swift 语法 - 以及学习资料

    附上一些swift的一下学习资料: 1.Swift语法介绍官方英文版:The Swift Programming Language 2.Swift与Objective-C相互调用Using Swift ...

  5. 数据库(批处理, 事务,CachedRawSetImpl类

    链接对象son产生的Statement SQL对象对数据库提交的任何一条语句都会被立刻执行 不方便我们进行一些连招操作 我们可以关闭它的自动提交,然后操作完再开,这过程称作事务 con.setAuto ...

  6. 07_DICTIONARY_ACCESSIBILITY

    07_DICTIONARY_ACCESSIBILITY 控制对系统权限的限制: TRUE 有相应系统权限,允许访问SYS下的对象. FALSE 确保拥有可以访问任何对象的系统权限,但不可以访问SYS下 ...

  7. angular的post传参后台php无法接收

    很多时候我们需要用ajax提交post数据,angularjs与jq类似,也有封装好的post. 但是jQuery的post明显比angularjs的要简单一些,人性化一些. 两者看起来没什么区别,用 ...

  8. 在Openstack上创建并访问Kubernetes集群

    第一部分:创建集群 在Openstack部署Kubernetes集群运行Nginx容器的步骤,其中包括: 利用Murano部署Kubernetes集群 配置Openstack的安全性使Kubernet ...

  9. (转)深度学习word2vec笔记之基础篇

    深度学习word2vec笔记之基础篇 声明: 1)该博文是多位博主以及多位文档资料的主人所无私奉献的论文资料整理的.具体引用的资料请看参考文献.具体的版本声明也参考原文献 2)本文仅供学术交流,非商用 ...

  10. Coursera, Big Data 2, Modeling and Management Systems (week 4/5/6)

    week4 streaming data format 下面讲 data lakes schema-on-read: 从数据源读取raw data 直接放到 data lake 里,然后再读到mode ...