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. 计算完成率 SQL

    计算完成率 SQL ,), ,) ) AS XX_完成率

  2. C C++ 文件输入与输出

    C语言: 一 打开关闭文件 1 fopen函数 用于打开文件 FILE *fopen(char *filename, *type); fopen("c:\\ccdos\\clib" ...

  3. Eclipse 创建和读取yaml文件

    工具和用法: 1. eclipse插件包:org.dadacoalition.yedit_1.0.20.201509041456-RELEASE.jar 用法:将此jar包复制到eclipse-jee ...

  4. Android density、dpi、dp、px

    Density DPI Example Device Scale Pixels ldpi 120 Galaxy Y 0.75x 1dp = 0.75px mdpi 160 Galaxy Tab 1.0 ...

  5. 【CF850E】Random Elections FWT

    [CF850E]Random Elections 题意:有n位选民和3位预选者A,B,C,每个选民的投票方案可能是ABC,ACB,BAC...,即一个A,B,C的排列.现在进行三次比较,A-B,B-C ...

  6. [工具] 知网(CNKI)文献下载工具

    https://github.com/amyhaber/cnki-downloader 用于免费搜索,下载CNKI上的各类文献资料

  7. IntelliJ IDEA导出Java 可执行Jar包

    extends:http://blog.sina.com.cn/s/blog_3fe961ae0102uy42.html 保证自己的Java代码是没有问题的,在IDEA里面是可以正常运行的,然后,按下 ...

  8. Asp.net MVC]Asp.net MVC5系列——Routing特性

    目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列— ...

  9. java基础/一个类A继承了类B,那么A就叫做B的派生类或子类,B就叫基类或超类。

    类重复,pulic class demo1 和class demo1 重复 无主类, 在cmd中输入命令: SET CLASSPATH=. (等号后为英文点符号),即可设置解释的路径为当前路径. 再次 ...

  10. POJ2387-Till the cows come home【最短路】

    A - Til the Cows Come Home POJ - 2387 Bessie is out in the field and wants to get back to the barn t ...