We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Yes, this is the correct solution. I don't know why everybody else is choosing such complicated solutions.
BTW you don't even need your check that "the index is different than the current index" if you do a single loop (for checking and adding) and wait to add items to the dict after you have already checked whether "remaining" is in the dict. It's also faster because you only loop once.
Here is my solution:
def icecreamParlor(m, arr):
seen = dict()
for i, cost in enumerate(arr):
if ((m - cost) in seen):
ans = [seen[m - cost] + 1, i + 1]
ans.sort()
return ans
seen[cost] = i
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
Yes, this is the correct solution. I don't know why everybody else is choosing such complicated solutions.
BTW you don't even need your check that "the index is different than the current index" if you do a single loop (for checking and adding) and wait to add items to the dict after you have already checked whether "remaining" is in the dict. It's also faster because you only loop once.
Here is my solution: