【问题标题】:Debug Google KickStart "2022" Round D Maximum Gain Problem using Dynamic Programming Technique使用动态规划技术调试 Google KickStart \"2022\" D 轮最大增益问题
【发布时间】:2023-01-27 00:08:46
【问题描述】:


我正在解决Google KickStart 2022 D 轮 - 最大增益问题通过动态规划技术但我厌倦了调试它。
这是问题的链接:Maximum Gain Problem
这是我的 python 代码:

#supporting function
def proceedForOneOnly(O, k, gainOne):
    if k==0:
        return 0
    return gainOne + max([proceedForOneOnly(O[1:], k-1, O[0]), proceedForOneOnly(O[:-1], k-1, O[-1])])

#solve
def maxGain(A, B, k, gain):
    #BaseCase
    if k==0:
        return 0
    #Checking if any of the two task-arrays are empty
    if len(B)==0:
        return proceedForOneOnly(A, k-1, gain)
    elif len(A)==0:
        return proceedForOneOnly(B, k-1, gain)
    #if both aren't empty
    return gain + max([maxGain(A[1:], B, k-1, A[0]), maxGain(A[:-1], B, k-1, A[-1]), maxGain(A, B[1:], k-1, B[0]), maxGain(A, B[:-1], k-1, B[-1])])

#taking input and caling maxGain() iteratively
def main():
    n = int(input())
    for i in range(n):
        nA = int(input()); A = list(map((lambda i: int(i)), input().split()))
        nB = int(input()); B = list(map((lambda i: int(i)), input().split()))
        k  = int(input())
        print(f"Case #{1}: {maxGain(A, B, k, 0)}")

main()

问题是我在下面附加的前两​​个测试用例中得到输出 22(应该是 24)和 138(应该是 148):

2
3
3 1 2
4
2 8 1 9
5
4
1 100 4 3
6
15 10 12 5 1 10
6

有人可以帮我弄清楚出了什么问题吗?

【问题讨论】:

    标签: python debugging dynamic max


    【解决方案1】:

    我认为您的循环运行时间少了一次。
    但是,运行它仍然需要很多时间,但是如果您可以在对我有用的 main 中的第一次调用中传递 k+1 而不是 k 。
    不错的方法顺便说一句。
    将尝试改进这一点

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2017-04-09
      • 1970-01-01
      • 2012-08-30
      • 2012-11-12
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2011-10-17
      相关资源
      最近更新 更多