[HackerRank] Electronics Shop

[문제 링크]

[입력]

int keyboards[n]: the keyboard prices array , size is n
int drives[m]: the drive prices array , size is m
int b: the budget

[출력]

int: the maximum that can be spent, or -1 if it is not possible to buy both items

[코드]

def getMoneySpent(keyboards, drives, b):

    result = 0
    keyboards.sort()
    drives.sort()
    
    if(keyboards[0]+drives[0]>b):
        return -1
    
    for i in range(len(keyboards)):
        for j in range(len(drives)):
            if(keyboards[i]+drives[j]<=b and result<= keyboards[i]+drives[j]):
                result = keyboards[i]+drives[j]
    return result

좋은 웹페이지 즐겨찾기