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 ...
随机推荐
- VMware虚拟机更换根用户( su: Authentication failure问题)
su命令不能切换root,提示su: Authentication failure,只要你sudo passwd root过一次之后,下次再su的时候只要输入密码就可以成功登录了.
- 解决MAC下修改系统文件没权限的问题
问题 用brew在mac上可以轻松的管理软件, 不过最新的mac系统升级后, brew执行update命令时会报权限不足的错误, 而且会提示执行命令sudo chown -R $(whoami) /u ...
- 十七、S3C2440 音频解码芯片WM8976声卡驱动移植、madplay测试
学习目标:1. WM9876接口和工作原理:2. WM9876驱动移植:3. WM9876应用测试:4. 问题总结 1. WM9876接口和工作原理 本节使用了JZ2440开发板移植WM9876驱动 ...
- 从0开始学golang--2.2--如何去爬园子的数据👉进阶篇,面向对象的单任务版
执行页main.go-----------------------------------代码
- 在jupyter中安装R的kernal
网上有安装完anaconda后可以直接使用conda 命令安装R的kernal,本人电脑上已经安装了anaconda和R,因此使用手动安装的方式安装. 安装环境: windows 8.1 企业版 An ...
- 目标反射回波检测算法及其FPGA实现 之三:平方、积分电路及算法的顶层实现
目标反射回波检测算法及其FPGA实现之三: 平方.积分电路及算法的顶层实现 前段时间,接触了一个声呐目标反射回波检测的项目.声呐接收机要实现的核心功能是在含有大量噪声的反射回波中,识别出发射机发出的激 ...
- LaTeX宏包TikZ绘图示例——Go语言起源图
本例所绘图形选自<Go语言程序设计>(作者:Alan A. A. Donovan与Brian W. Kernighan)一书的前言部分. 完整代码 \documentclass{art ...
- JavaWeb总结(一)
在学习Web应用程序客户端界面设计时,我们已经知道组成一个基本的Web应用程序需要Web服务器.Web客户端浏览器.HTTP协议以及静态HTML文件. Web服务器:接收客户端请求,然后向客户端返回一 ...
- C++ STL 学习笔记__(5)list
10.2.6List容器 List简介 ² list是一个双向链表容器,可高效地进行插入删除元素. ² list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符.It++(ok) i ...
- vim使用技巧(插入,删除,查找,复制,粘贴,剪切)
原文链接:http://blog.csdn.net/qq_38646470/article/details/79643000 编程人员很喜欢的编辑器:vim 先搞清楚vim的三种模式: 1.命令模式: ...