1

我最近开始做这种比赛,我不明白为什么我的解决方案不能通过谷歌的问题。

问题:

https://codingcompetitions.withgoogle.com/kickstart/round/0000000000435bae/0000000000887c32

解决方案:

def Solution(house : int, location : str) -> int:

lastHousePos = 0
nextHousePos = 0
res = 0

#Tried it using two pointers
for i in range(house):
    nextHousePos = location.find('1', i + 1)
    if location[i] == '0':
        if location[lastHousePos] == '0':
            res += max(abs(i - lastHousePos), abs(i - nextHousePos))
        else:
            res += min(abs(i - lastHousePos), abs(i - nextHousePos))
    else:
        lastHousePos = i


    # print("last " + str(lastHousePos))
    # print("next " + str(nextHousePos))
    # print("res " + str(res))

return res

它通过了样本测试,但没有通过样本集 1。我很好奇我的错误是什么,因为 Google 拒绝向我提供导致问题的输入。感谢您的帮助和阅读!

4

0 回答 0