我嘗試在 HackerRank 上解決一個名為“Apple and orange”的問題,代碼如下:
def countApplesAndOranges(s, t, a, b, apples, oranges):
count_apples = 0
count_oranges = 0
x = [x for x in range(s, t 1)]
pos_apple = [apple a for apple in apples]
pos_orange = [orange b for orange in oranges]
for i in x:
for j in pos_apple:
if j == i:
count_apples =1
for l in pos_orange:
if l == i:
count_oranges = 1
print(count_apples)
print(count_oranges)
該代碼有效。但是,當我嘗試提交它時,它通過了前 3 個測驗,其余測驗失敗,但出現例外“因超時而終止”。我檢查了其中一項測驗的輸入,它需要處理大量資料,您可以在此處查看資料:https : //hr-testcases-us-east-1.s3.amazonaws.com/ 25220/input03.txt?AWSAccessKeyId=AKIAR6O7GJNX5DNFO3PV&Expires=1642016820&Signature=J4ypdP0YzRxcOWp+y5XaD5ITeMw=&response-content-type=text/plain
它失敗了,因為通過我的 IDE 處理具有相同輸入的代碼需要大約 2 分鐘,但 HackerRank 測驗限制為 10 秒。
我需要你的幫助來優化代碼并讓它運行得更快。
嵌套回圈似乎是這里最大的問題,但我不知道應該用什么替換。
uj5u.com熱心網友回復:
檢查對每個每個蘋果/橙坐標范圍內將你的代碼的運行時復雜到O(n * a n * o)哪里n是家之長,a是蘋果的數量,o是橘子的數量。理想情況下,您的代碼必須在O(a o).
這是您的解決方案的重構版本:
def countApplesAndOranges(s, t, a, b, apples, oranges):
count_apples = 0
count_oranges = 0
for apple in pos_apple:
if s <= apple a <= t:
count_apples =1
for orange in pos_orange:
if s <= orange b <= t:
count_oranges = 1
print(count_apples)
print(count_oranges)
uj5u.com熱心網友回復:
如果可以,請不要構建中間串列,如果確實需要這樣做(這不是問題的情況),請改用生成器
并且盡量不要重復自己,盡可能用函式分解
def countApplesAndOranges(home_start, home_end, tree_apple, tree_orange, apples, oranges):
def count_fruits(home_start, home_end, tree, fruits):
count = 0
for fruit in fruits:
if home_start <= tree fruit <= home_end:
count =1
return count
print(count_fruits(home_start, home_end, tree_apple, apples))
print(count_fruits(home_start, home_end, tree_orange, oranges))
uj5u.com熱心網友回復:
我想我會sum()以幾個串列推導作為起點:
apple_hits = sum(
1 for apple in apples
if house_min <= apple apple_tree_origin <= house_max
)
不過,向我指出的一件事是,從該測驗的兩側減去 apple_tree_origin 不應該改變它:
apple_hits = sum(
1 for apple in apples
if house_min - apple_tree_origin <= apple <= house_max - apple_tree_origin
)
現在我們可能會觀察到house_min - apple_tree_origin可以預先計算,剩下的我的答案是:
def countApplesAndOranges1(s, t, a, b, apples, oranges):
house_min = s
house_max = t
apple_tree_origin = a
orange_tree_origin = b
house_min_apples = house_min - apple_tree_origin
house_max_apples = house_max - apple_tree_origin
house_min_oranges = house_min - orange_tree_origin
house_max_oranges = house_max - orange_tree_origin
apple_hits = sum(1 for apple in apples if house_min_apples <= apple <= house_max_apples)
orange_hits = sum(1 for orange in oranges if house_min_oranges <= orange <= house_max_oranges)
return apple_hits, orange_hits
有了你提供的測驗資料,我得到了(18409, 19582)。希望這是正確的。
隨意timeit反對其他解決方案:
import timeit
setup = """
with open("apples_oranges.txt") as file_in:
s,t = list(map(int, file_in.readline().split()))
a,b = list(map(int, file_in.readline().split()))
m,n = list(map(int, file_in.readline().split()))
apples = list(map(int, file_in.readline().split()))
oranges = list(map(int, file_in.readline().split()))
def countApplesAndOranges_jonsg(s, t, a, b, apples, oranges):
house_min = s
house_max = t
apple_tree_origin = a
orange_tree_origin = b
house_min_apples = house_min - apple_tree_origin
house_max_apples = house_max - apple_tree_origin
house_min_oranges = house_min - orange_tree_origin
house_max_oranges = house_max - orange_tree_origin
apple_hits = sum(1 for apple in apples if house_min_apples <= apple <= house_max_apples)
orange_hits = sum(1 for orange in oranges if house_min_oranges <= orange <= house_max_oranges)
return apple_hits, orange_hits
"""
print(timeit.timeit("countApplesAndOranges_jonsg(s, t, a, b, apples, oranges)", setup=setup, number=1000))
uj5u.com熱心網友回復:
感謝你們!
我想通了,并使用了
if s <= apple a <= t:
在我的代碼中,因為它是擺脫嵌套回圈的最簡單的解決方案。它作業得很好,讓我想知道我怎么沒有想到它。
我真的很喜歡您的所有解決方案,并感謝您的幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409367.html
標籤:
上一篇:洗掉值后重新計算總體方差的公式
