股票买卖的数据结构查询

问题描述

我有一个关于股票买卖的问题。请参考此链接https://practice.geeksforgeeks.org/problems/stock-buy-and-sell/0/?company[]=Facebook&difficulty[]=1&page=1&query=company[]Facebookdifficulty[]1page1#

有人可以告诉我我的代码有什么问题吗?我已经将输入内容粘贴到了代码中。它不适用于这种情况。输出中的最后一对未显示在预期输出中。 def stockbuysell(price,n):

maxArray=[]
cnt=0
localMinima=-1
localMaxima = -1
cntMin = 0
lowestMinimum=0
print "cnt",cnt
print "n",n-1
while (cnt < n):
    if localMinima == -1:

        localMinima= price[cnt]           
        localMaxima=price[cnt]
        cntMin = cnt            
        cnt+=1
        continue
            if cnt == n and localMaxima ==-1:
        break        
    if price[cnt] < localMinima and price[cnt] > localMaxima:
        cntMin=cnt
        localMinima=price[cnt]            
        cnt += 1
        continue
    if cnt == n-1:            
        localMaxima =price[cnt]
        maxArray.append([localMinima,localMaxima])            
        break
    if price[cnt] > localMaxima:
        localMaxima=price[cnt]   
    else:
        maxArray.append([cntMin,cnt-1])
        localMinima=price[cnt]            
        localMaxima=-1
        cntMin = cnt
    cnt+=1
strng=""
for dd in maxArray:
    strng+="("+str(dd[0])+" "+str(dd[1])+") "
return strng

如果名称 ==“ 主要”:

t=1
while (t > 0):    

    price = "100 180 260 310 40 535 695"
    price ="886 2777 6915 7793 8335 5386 492 6649 1421 2362 27 8690 59 7763 3926 540 3426 9172 5736 5211 5368 2567 6429 5782 1530 2862 5123 4067 3135 3929 9802 4022 3058 3069 8167 1393 8456 5011 8042 6229 7373 4421 4919 3784 8537 5198 4324 8315 4370 6413 3526 6091 8980 9956 1873 6862 9170 6996 7281 2305 925 7084 6327 336 6505 846 1729 1313 5857 6124 3895 9582 545 8814 3367 5434 364 4043 3750 1087 6808 7276 7178 5788"

    price = [int(x) for x in price.split()]         
    t -= 1

在极客上运行极客练习结果:

答案错误。 !!!错误的答案

可能您的代码在多个测试用例(TC)中无法正常工作。

代码失败的第一个测试用例:

输入: 84 886 2777 6915 7793 8335 5386 492 6649 1421 2362 27 8690 59 7763 3926 540 3426 9172 5736 5211 5368 2567 6429 5782 1530 2862 5123 4067 3135 3929 9802 4022 3058 3069 8167 1393 8456 5011 8042 6229 7373 4421 4919 3784 5537 4198 8324 4315 64 3526 6091 8980 9956 1873 6862 9170 6996 7281 2305 925 7084 6327 336 6505 846 1729 1313 5857 6124 3895 9582 545 8814 3367 5434 364 4043 3750 1087 6808 7276 7178 5788

其正确的输出是: (0 4)(6 7)(8 9)(10 11)(12 13)(15 17)(19 20)(21 22)(24 26)(28 30)(32 34)(35 36)(37 38)(39 40)(41 42)(43 44)(46 47)(48 49)(50 53)(54 56)(57 58)(60 61)(63 64)(65 66)(67 69) (70 71)(72 73)(74 75)(76 77)(79 81)

您的代码输出为: (0 4)(6 7)(8 9)(10 11)(12 13)(15 17)(19 20)(21 22)(24 26)(28 30)(32 34)(35 36)(37 38)(39 40)(41 42)(43 44)(46 47)(48 49)(50 53)(54 56)(57 58)(60 61)(63 64)(65 66)(67 69) (70 71)(72 73)(74 75)(76 77) 预期输出

我的输出

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)