[英]How to reduce the run - time of python program ( program takes a lot of time in execution)
我正在解决 hackerank 上的小黄人游戏问题,但我没有通过代码运行时间过多的测试用例。 代码是:
def minion_game(word):
# initialize the scores of player 1 and 2 to 0
# initialize the jump for index
# initialize a string of vowels
scoreOfStuart, scoreOfKevin, adder, vowels = 0, 0, 1, "AEIOU"
i = 0
while i < len(word):
# initialize a list to store all the possible substrings for each length of substrings
listOfSubStrings = []
listOfSubStrings = [word[j : j + adder] for j in range(len(word) - i)]
# end nested for loop ---- now a list of all possible substrings is present
# another nested for loop to determine the score
listOfStuart = [j for j in listOfSubStrings if j[0] not in vowels]
listOfKevin = [j for j in listOfSubStrings if j[0] in vowels]
scoreOfStuart += len(listOfStuart)
scoreOfKevin += len(listOfKevin)
# increment in adder by 1
adder += 1
i += 1
# printing the scores
if scoreOfStuart > scoreOfKevin:
print("Stuart", scoreOfStuart)
elif scoreOfStuart < scoreOfKevin:
print("Kevin", scoreOfKevin)
else:
print("Draw")
if __name__ == "__main__":
string = input().upper()
minion_game(string)
我尝试了列表理解而不是 for 循环来将字符串附加到列表中,并且尝试了简单的 while 循环而不是 main for 循环,运行时间仍然没有减少。 任何帮助将不胜感激。
除了检查第一个字母外,您根本不使用子字符串,因此您可以摆脱对它们的全部计算:
def minion_game(word):
scoreOfStuart, scoreOfKevin, vowels = 0, 0, set("AEIOU")
for i in range(len(word)):
for j in range(len(word)-i)):
if word[j] in vowels:
scoreOfKevin += 1
else:
scoreOfStuart += 1
# printing the scores
if scoreOfStuart > scoreOfKevin:
print("Stuart", scoreOfStuart)
elif scoreOfStuart < scoreOfKevin:
print("Kevin", scoreOfKevin)
else:
print("Draw")
此外,您会注意到您也不需要执行嵌套循环,您只需将单词迭代一次即可,因为有len(word)-i
字符串以相同的字母开头。 例如word banana
和i==1
,有子串a
, an
, ana
, anan
, anana
总共len(word)-i == 6 - 1 == 5
,这样你就可以直接把这个分数加到Kevin上
def minion_game(word):
scoreOfStuart, scoreOfKevin, vowels = 0, 0, set("AEIOU")
for i in range(len(word)):
if word[i] in vowels:
scoreOfKevin += len(word)-i
else:
scoreOfStuart += len(word)-i
# printing the scores
if scoreOfStuart > scoreOfKevin:
print("Stuart", scoreOfStuart)
elif scoreOfStuart < scoreOfKevin:
print("Kevin", scoreOfKevin)
else:
print("Draw")
问题不在于使用 for lop 或列表理解或 while 循环。 所有这些方法都取决于单词的长度。 让我们说n。 所以在你所有的算法中,时间复杂度都是 o(n)。 Hackerrank 希望你在小于 o(n) 的时间内解决这个问题。
在此处了解有关时间复杂度的更多信息
在竞争性编程中的任何问题中,都可以根据对输入的约束对算法的时间复杂度进行一些估计:
| Constraint | Required Time Complexity |
|-----------------|--------------------------------------------|
| n <= 10 | O(n!) |
| n <= 20 | O(2n) |
| n <= 500 | O(n3) |
| n <= 5000 | O(n2) |
| n <= 10^6 | O(n) or O(n log n) |
| n can be > 10^6 | O(1) or O(log n) (and in some cases O(√n)) |
这应该让您直观地了解到 go 的方式。
在这里,您将生成所有子字符串并根据第一个字母进行过滤。 我们真的需要子串还是只需要计数。 从问题https://www.hackerrank.com/challenges/the-minion-game/problem可以重复(ANA 在 BANANA 中出现两次。因此,Kevin 将获得 2 分)。
解决方案:考虑字符串的第一个字母,您可以查看有多少子字符串组合以您正在迭代的字母开头,并将该数字添加到 kevin 或 stuart,而不是一次添加一个。 如果你需要代码。 最好的地方将是问题的讨论部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.