Whats wrong with the code? It result in RE in google Kickstart 2020 Round A

Viewed 789

What i am missing here in this code? It results in RE in Kickstart 2020 but when i test on my local machine or hackereath compiler(similar to codejam complier) code works fine.
problem link: https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56

def solve(a,balance):
    a.sort()
    house_Can_buy=0
    for i in a:
        if i<=balance:
            house_Can_buy+=1
            balance-=i
    return house_Can_buy
def main():
    a=int(input())
    arr2,res=[],[]
    for i in range(a):
        _,balance=list(map(int,input().split()))
        arr2=list(map(int,input().split()))
        result=solve(arr2,balance)
        res.append(result)
    for i,j in enumerate (res):
        print(f'Case #{i+1}: {j}')
main()

1 Answers

At the print statement in which use of f is resulting in RE.

In the print statement, you should use format to output i+1 and j.

To pass all the test cases you have to sort the array to calculate number of houses by the greedy method.

def solve(a,balance):
    house_Can_buy=0
    a.sort()
    for i in a:
        if i<=balance:
            house_Can_buy+=1
            balance-=i
    return house_Can_buy

def main():
    a=int(input())
    arr2,res=[],[]
    for i in range(a):
        _,balance=list(map(int,input().split()))
        arr2=list(map(int,input().split()))
        result=solve(arr2,balance)
        res.append(result)
    for i,j in enumerate (res):
        print('Case #{}: {}'.format(i+1,j))
main()
Related