python代码不能在数组中工作
这是一个算法问题,其主题是: 买卖股票的最佳时机 假设您有一个数组,其第i个元素是第一天给定股票的价格。 如果你最多只能完成一笔交易(即,买入一股,卖出一股),那么设计一个算法来寻找最大利润 *示例1: 输入:[7,1,5,3,6,4] 产出:5 最大差异=6-1=5(不是7-1=6,因为销售价格需要大于购买价格)* *示例2: 输入:[7,6,4,3,1] 输出:0 在这种情况下,不进行任何交易,即最大利润=0* 我用python解决了这个问题。代码如下:python代码不能在数组中工作,python,arrays,algorithm,Python,Arrays,Algorithm,这是一个算法问题,其主题是: 买卖股票的最佳时机 假设您有一个数组,其第i个元素是第一天给定股票的价格。 如果你最多只能完成一笔交易(即,买入一股,卖出一股),那么设计一个算法来寻找最大利润 *示例1: 输入:[7,1,5,3,6,4] 产出:5 最大差异=6-1=5(不是7-1=6,因为销售价格需要大于购买价格)* *示例2: 输入:[7,6,4,3,1] 输出:0 在这种情况下,不进行任何交易,即最大利润=0* 我用python解决了这个问题。代码如下: class Solution(obj
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
i=Max=0
if not prices:
return 0
while i+1 <= len(prices)-1:
j=i+1
while j < len(prices):
if prices[i] < prices[j]:
Max = max(Max, prices[j] - prices[i])
j+=1
j+=1
i+=1
return Max
类解决方案(对象):
def maxProfit(自身、价格):
"""
:类型价格:列表[int]
:rtype:int
"""
i=最大值=0
如果不是价格:
返回0
而i+11j无需增加
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
i=Max=0
if not prices:
return 0
while i+1 <= len(prices)-1:
j=i+1
while j < len(prices):
if prices[i] < prices[j]:
Max = max(Max, prices[j] - prices[i])
j+=1
i+=1
return Max
输出:
5
0
3
干得好!虽然代码可能会有一些改进,但让我们关注一个导致返回错误结果的bug:
Max = max(Max, prices[j] - prices[i])
j+=1
j+=1
这是双j+=1
。每当最大值更改时,j
将增加两次,跳过一些比较
移除if
-分支中的j+=1
,您将获得输入向量的正确结果:
Max = max(Max, prices[j] - prices[i])
j+=1
如果有兴趣,以下是一些改进编码风格的技巧:
根据我的理解,虽然我+1,但我相信你必须将问题重新表述并调整为代码。我基于示例和预期结果的观察结果:
- 购买价格在输入列表中,必须确定
- 售价也在输入列表中,必须确定
- 买价必须低于卖价
- 利润是产出,因此是买卖
现在,将其转换为算法:
尝试1(在某些情况下错误-参见注释)
在清单中找出最低价格——这是我们的购买价格
在列表中找出“同一天或以后”的最大值,即指数等于或大于购买价格指数
代码(单个可执行文件-如果需要,需要将其设置为函数):
示例:
$ /tmp/stock.py 1,2,4
Input: [1, 2, 4]
Output: 3
$ /tmp/stock.py 7,1,5,3,6,4
Input: [7, 1, 5, 3, 6, 4]
Output: 5
$ /tmp/stock.py 1,2,3,4,5
Input: [1, 2, 3, 4, 5]
Output: 4
$ /tmp/stock.py 4,3,2
Input: [4, 3, 2]
Output: 0
$ /tmp/stock.py 4,3,1
Input: [4, 3, 1]
Output: 0
$ /tmp/stock.py 4,3,5
Input: [4, 3, 5]
Output: 2
基于@kazemakase输入的尝试2
购买价格在清单中,但不一定是最低价格。利润最大化的是价值李>
对于每一天,计算如果我们当天购买股票,我们的利润会是多少——卖出价格是指数大于当天的最大值
(@kazemakase)循环有时是不可避免的
守则:
#!/usr/bin/env python
import sys
prices = map(int, sys.argv[1].split(","))
# For every day
buy_final = 0
sell_final = 0
max_profit = 0
for (buyindex, buy) in enumerate(prices):
sell = max(prices[buyindex:])
profit = sell - buy
if profit > max_profit:
max_profit = profit
buy_final = buy
sell_final = sell
print(" Input: %s" % str(prices))
print("Output: %d" % (sell_final-buy_final))
结果是:
$ /tmp/stock.py 7,1,5,3,6,4
Input: [7, 1, 5, 3, 6, 4]
Output: 5
$ /tmp/stock.py 1,2,4
Input: [1, 2, 4]
Output: 3
$ /tmp/stock.py 3,6,1,2
Input: [3, 6, 1, 2]
Output: 3
如果您需要进一步澄清,或者如果我的假设错误,请告知我类解决方案(对象):
def maxProfit(自身、价格):
"""
:类型价格:列表[int]
:rtype:int
"""
i=最大值=0
如果不是价格:
返回0
当I+ 1<P>当我们遍历价格表时,让我们把第一个之后的每一个值视为一个潜在的销售价格。理想的购买价格是多少
提示:我们已经看到了它,并且它可以随着时间和空间的推移而更新(这是我们在现实世界中通常没有的好处:)。告诉我我是否理解了正确的逻辑:如果不是价格,那么就不要卖aka 0:else(第二个最大)-最小价格?不,不。。“if not prices”表示执行语句“if”,如果数组价格为空或非法,则不执行任何操作,只返回0。避免循环始终是一个好主意,但此解决方案在这样的情况下返回错误的结果,即最低价格不是最佳购买:prices=[3,6,1,2]
(返回1而不是3-假设你必须先买后卖)@kazemakase:捕捉得很好-我觉得它太简单了…:)@kazemakase:我想现在已经修复了@kazemakase:你说得对-lambda看起来像perl!我不想在我的任何项目中看到这一点。。。单行:print(“输出:%d”%max(映射(lambda(k,v):max(价格[k:])-v,枚举(价格)))
也感谢您兄弟。你的想法很好,你的评论很有效。我确实又加了一个j+=1。非常感谢,非常感谢兄弟。你的小费对我很好。
$ /tmp/stock.py 1,2,4
Input: [1, 2, 4]
Output: 3
$ /tmp/stock.py 7,1,5,3,6,4
Input: [7, 1, 5, 3, 6, 4]
Output: 5
$ /tmp/stock.py 1,2,3,4,5
Input: [1, 2, 3, 4, 5]
Output: 4
$ /tmp/stock.py 4,3,2
Input: [4, 3, 2]
Output: 0
$ /tmp/stock.py 4,3,1
Input: [4, 3, 1]
Output: 0
$ /tmp/stock.py 4,3,5
Input: [4, 3, 5]
Output: 2
#!/usr/bin/env python
import sys
prices = map(int, sys.argv[1].split(","))
# For every day
buy_final = 0
sell_final = 0
max_profit = 0
for (buyindex, buy) in enumerate(prices):
sell = max(prices[buyindex:])
profit = sell - buy
if profit > max_profit:
max_profit = profit
buy_final = buy
sell_final = sell
print(" Input: %s" % str(prices))
print("Output: %d" % (sell_final-buy_final))
$ /tmp/stock.py 7,1,5,3,6,4
Input: [7, 1, 5, 3, 6, 4]
Output: 5
$ /tmp/stock.py 1,2,4
Input: [1, 2, 4]
Output: 3
$ /tmp/stock.py 3,6,1,2
Input: [3, 6, 1, 2]
Output: 3