Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题:
Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). 贪心实现如下:
'''
Created on Nov 13, 2014
@author: ScottGu<gu.kai.66@gmail.com, kai.gu@live.com>
'''
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
self.__init__()
for i in range(len(prices)):
prices[i] = prices[i] + 1
prices.append(0) self.trade(prices)
return self.profit
def __init__(self):
self.profit = 0
self.bought = 0
def trade(self, prices):
if (prices == None):
return
for i in range(1, len(prices) - 1):
if (prices[i - 1] < prices[i] >= prices[i + 1]): # sell
if (self.bought == 0): self.bought = prices[i - 1]
self.profit += (prices[i] - self.bought)
self.bought = 0 if (prices[i - 1] >= prices[i] < prices[i + 1]): # buy
self.bought = prices[i] if (prices[i - 1] < prices[i] < prices[i + 1]): # maybe buy
if (self.bought == 0): self.bought = prices[i - 1]
if (self.bought > 0):
self.profit += (prices[-1] - self.bought) if __name__ == '__main__':
so = Solution()
# test cases:
prices = [1, 2, 3, 4, 5, 3, 3, 3, 2, 6, 7, 3, 4]
print prices
print so.maxProfit(prices)
# case 2
prices = [1, 2]
print prices
print so.maxProfit(prices)
# case 3
prices = [2, 2, 5]
print prices
print so.maxProfit(prices)
贪心算法的特点是一条路走到黑,把问题分解成若干子问题,逐个解决,问题集越来越小直到全解完,这时结果集就认为是最优解。
但贪心算法并不能在所有场景下确保结果是最优解,在一些情况下结果是次优解,看这个问题:
假如某个国家只有1元、5元和11元面值的钞票,这时如果有商人要【找零15元】,问最少钞票张数?
假如使用贪心算法,则结果为一张11元和4张1元钞票,共5张。而实际正确结果应该为3张5元钞票。
那么问题来了,在什么场景下使用贪心算法能获得最优解?
答:局部最优解能决定全局最优解。简单地说,问题能够分解成子问题来解决,子问题的最优解能递推到最终问题的最优解。
贪心法一般不能得到我们所要求的答案。一旦一个问题可以通过贪心法来解决,那么贪心法一般是解决这个问题的最好办法。由于贪心法的高效性以及其所求得的答案比较接近最优结果,贪心法也可以用作辅助算法或者直接解决一些要求结果不特别精确的问题。
Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)的更多相关文章
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [LeetCode] Best Time to Buy and Sell Stock II 贪心算法
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode: Best Time to Buy and Sell Stock II [122]
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- LeetCode——Best Time to Buy and Sell Stock II
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- LeetCode OJ--Best Time to Buy and Sell Stock II
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 第二问,是说可以进行无数次买卖. 贪心法 #include &l ...
随机推荐
- Mac app打包成dmg
1. 找到打包的app文件,在Xcode工程的Products目录下 2. 在桌面新建一个文件夹dmg,将app文件放进去. 3. 为了实现拖拽到Application的效果,需要在这个文件夹里放一个 ...
- Lambda表达式学习(1)
项目里面需要经常对一系列同类型集合进行操作 , 如对集合进行增加元素 , 删除集合的指定索引的元素等等.我们可以使用ArrayList来进行. 如 ArrayList stringArrayLis ...
- bootstrap 多级下拉菜单
如上效果: 实现代码: 导入js和css: <link rel="stylesheet" href="http://cdn.static.runoob.com/li ...
- 【绝密】为什么现在的CAN收发器通信距离越来越短?
[绝密]为什么现在的CAN收发器通信距离越来越短? CAN收发器的改良和隔离器件引入,大大提高了通信的可靠性,但同时也引入了额外的延时,导致通信距离变短,或总线错误帧增加,本文以1Mbps波特率下 ...
- C#窗口皮肤制作(二):创建窗口库项目以及最小化、最大化、关闭button的实现
非常高兴有朋友关注这篇博客,同一时候也十分抱歉让关注的朋友久等了,隔上一篇博客也有3个月没有更新,主要是因为3月份辞职,4月份初离职到期离开了北京高德,来到了上海张江.眼下新工作也处于熟悉其中,希望大 ...
- Linux Shell常用技巧(四)
九. awk实用功能: 和sed一样,awk也是逐行扫描文件的,从第一行到最后一行,寻找匹配特定模板的行,并在这些行上运行“选择”动作.如果一个模板没有指定动作,这些匹配的行就被显示在屏幕上.如果一 ...
- MongoDB相关操作
1. 连接MongoDB <?php //1.连接到MongoDB $host = "127.0.0.1"; $port = 27017; $server = " ...
- 单片机实现简易版shell的方法和原理
Rt-thread 中有一个完整的finsh(shell )系统,使用串口做命令行输入输出.但是想要用这个炫酷的工具就必须要上rtthread系统,或者花大力气将其移植出来.于是我就自己写了一个类似于 ...
- zabbix和elasticsearch数据表
# uint mapping { "settings" : { "index" : { "number_of_replicas" : 1, ...
- GoLand Dep Integration Project
Dep integration 标签(空格分隔): Go 1. 通过 GoLand 使用 dep integration 创建项目 参见 Dep integration 使用 dep integrat ...