Python 3.x 优化代码,使其在5秒内运行

Python 3.x 优化代码,使其在5秒内运行,python-3.x,optimization,mathematical-optimization,Python 3.x,Optimization,Mathematical Optimization,在HackerEarth上的一些测试用例中,以下代码花费的时间超过5秒(5.001)。如何对该代码进行更优化,使其在5秒内运行 tc = int(input()) ip = [] for x in range(0, tc): temp = [] temp.append(int(input())) temp.append([int(n) for n in input().split()]) temp.append(int(input())) ip.appen

在HackerEarth上的一些测试用例中,以下代码花费的时间超过5秒(5.001)。如何对该代码进行更优化,使其在5秒内运行

tc = int(input())
ip = []
for x in range(0, tc):
    temp = []
    temp.append(int(input()))
    temp.append([int(n) for n in input().split()])
    temp.append(int(input()))
    ip.append(temp)

for it in ip:
    while not it[2] <= 0:
        for x in range(0, it[0]):
            if it[1][x] == '0':
                continue
            it[2] -= int(it[1][x])
            if it[2] <= 0:
                it.append(x+1)
                break
    print(it[3])
tc=int(输入())
ip=[]
对于范围(0,tc)内的x:
温度=[]
临时追加(int(input()))
临时追加([int(n)表示input().split()]中的n)
临时追加(int(input()))
ip.append(临时)
对于ip中的it:

虽然不是[2]假设这个问题来自哪里,但我们的目标是找出在一年中的哪一天达到里程碑。因此,我们只对实现这一里程碑所需的最后一年感兴趣

因此,只需使用操作员找出去年的剩余距离,即可大幅提高代码速度:

remainder = target % dist_per_year
然后,可以使用与最初使用的方法相同的方法对剩余部分进行迭代。示例(用python 2.7编写):


多亏了J.Hollom,我做了一些更改,并将代码移植到了python3.x。请注意,我的代码是为Hacker Earth设计的(链接由J.Hollom提供),因此它需要输入

test_cases = int(input())
inputs = []
for x in range(test_cases):
    temp = []
    temp.append(int(input()))
    temp.append([int(n) for n in input().split()])
    temp.append(int(input()))
    temp.append(sum(temp[1]))
    inputs.append(temp)


for item in inputs:
    item[2] = item[2] % item[3]
    if item[2] == 0:
        item[2] += item[3]
    day = 0
    while item[2] > 0:
        item[2] -= item[1][day]
        day += 1 # Since day begins at Day 1 this will give right answer!
    print(day)

好了,现在所有的输入在不到1秒的时间内运行

您是否尝试过multiprocessing.pool()工具?您将输入列表拆分为您的机器有多少可用CPU。这将大大加快速度。请参阅此处的文档,我猜您忘记了余数为0时的情况?您不能简单地返回dist的长度。请说
dist=[5,6,0,0,0]
,如果余数为0,那么答案将是第2天而不是第5天!最好的方法可能是每年为
dist\u运行一次循环!检查下面我的答案!
import random as rand
import time


def testcase(dist, milestone):
    dist_per_year = sum(dict)
    remainder = milestone % dist_per_year

    if remainder == 0:
        return len(dist)

    day = 0
    while remainder > 0:
        day = day + 1
        remainder = remainder - dist[day - 1]

    return day


milestone = rand.randint(0, 10e16)
days = rand.randint(1, 10e5)
dist = [rand.randint(0, 10e8) for i in xrange(days)]

t0 = time.time()
day = testcase(dist, milestone)
print 'Day:', day
print 'Time:', time.time() - t0
test_cases = int(input())
inputs = []
for x in range(test_cases):
    temp = []
    temp.append(int(input()))
    temp.append([int(n) for n in input().split()])
    temp.append(int(input()))
    temp.append(sum(temp[1]))
    inputs.append(temp)


for item in inputs:
    item[2] = item[2] % item[3]
    if item[2] == 0:
        item[2] += item[3]
    day = 0
    while item[2] > 0:
        item[2] -= item[1][day]
        day += 1 # Since day begins at Day 1 this will give right answer!
    print(day)