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 ...
随机推荐
- 集合之subList的缺陷
我们经常使用subString方法来对String对象进行分割处理,同时我们也可以使用subList.subMap.subSet来对List.Map.Set进行分割处理,但是这个分割存在某些瑕疵. 一 ...
- 拼多多java后台笔试题目总结(20180830)
1.回合攻击问题 package com.hone.pdd; import java.util.Scanner; /** * 题目:模拟一个游戏场景,两种伤害,一种正常伤害,一种是先蓄力(也算一个回合 ...
- w10下Oracle 11g完全干净卸载
1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> H ...
- C# lambda表达式参数的正确使用姿势
C#的lambda表达式的好用就不多说了,中午吃饭的时候突然想到一个以前(有年头了,难道屌丝上岁数了就回忆这个么...)和同事争执的坑.. 列个demo吧.. 先是一个类,这个类的对象就是为了吃堆内存 ...
- unlink与close关系
close和unlink.以前时候总是不太理解两者的区别,最近看到一篇博客比较详细地描述了二者的本质区别,这里我引用了它的原文. “每一个文件,都可以通过一个struct stat的结 ...
- 01.创建winserver2012r2+hyper-v+centos7
1.背景 DELL poweredge T320,装的winserver2012 r2,利用自带的hyper-v安装centos7,后期主要用于spark开发. 1.1 安装winserver2012 ...
- msserver的update or insert语句
方案1:SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; IF EXISTS (SELECT 1 FROM dbo.ta ...
- ZooKeeper实现分布式队列Queue
ZooKeeper实现分布式队列Queue 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让Hadoop集群跑在VPS虚拟主机上,通过云向用户提供存储和计算的服务. 现在硬件越来 ...
- 9.13 开课第十天(JS脚本语音:语句:循环)
循环:循环操作某一个功能(执行某段代码) 四要素: 循环初始值 循环条件 状态改变 循环体 for 穷举 迭代 while(true) break //先执行 ...
- 实现后门程序以及相应的rootkits,实现对后门程序的隐藏
iptables的一些命令: a. a) 使用规则实现外网不能访问本机,但是本机主动发起的连接正常进行. sudo iptables –A INPUT -p tcp —tcp —syn -j D ...