[LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
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的更多相关文章
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- Leetcode 121. Best Time to Buy and Sell Stock 最佳股票售卖时(动态规划,数组,模拟)
题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...
随机推荐
- Qt编写数据库通用翻页demo(开源)
在Qt与数据库结合编程的过程中,记录一多,基本上都需要用到翻页查看记录,翻页有个好处就是可以减轻显示数据的表格的压力,不需要一次性将数据库表的记录全部显示,也基本上没有谁在一页上需要一次性显示所有记录 ...
- VIM 如何使用系统的剪切板
想要将系统剪贴板里的内容复制到 vi 编辑的文档中怎么办? 例如,在网页上复制了一段文字,想贴到本地的某个文件中. 使用 vi 打开本地文件,在 输入 模式下,按 Shift + Insert 详细可 ...
- HTML5 css3 阴影效果
阴影效果曾让 Web 设计师既爱又恨,现在,有了 CSS3,你不再需要 Photoshop,已经有网站在使用这个功能了,如 24 Ways website. -webkit-box-shadow: 1 ...
- Elasticsearch学习之深入搜索一 --- 提高查询的精准度
1. 为帖子增加标题字段 POST /forum/article/_bulk { "} } { "doc" : {"title" : "th ...
- zabbix配置server,proxy,agent架构
author: headsen chen date:2018-10-30 19:49:50 环境: centos 6.8_x86_64 zabbix-server: 192.168.1.130 z ...
- np.tile 函数使用
>>> import numpy>>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次array([0, 0, 0, 0, 0, ...
- HDU 2476 String painter(区间DP)
String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- TOP100summit:【分享实录】链家网大数据平台体系构建历程
本篇文章内容来自2016年TOP100summit 链家网大数据部资深研发架构师李小龙的案例分享. 编辑:Cynthia 李小龙:链家网大数据部资深研发架构师,负责大数据工具平台化相关的工作.专注于数 ...
- 实例Python处理XML文件的方法
转自:http://www.jb51.net/article/71773.htm 以下是部分代码: <?xml version="1.0" encoding="UT ...
- paas平台
paas平台 定义:PaaS是云计算中重要的一类服务,为用户提供应用的全生命周期管理和相关的资源服务.通过PaaS,用户可以完成应用的构建.部署.运维管理,而不需要自己去搭建计算环境,如安装服务器.操 ...