Can someone explain why the max function works in first scenario and not the second?
First that works:
class Solution(object):
def kidsWithCandies(self, candies, extraCandies):
“””
:type candies: List[int]
:type extraCandies: int
:rtype: List[bool]
“””
result = candies
maxCandies = max(candies)
for i in range(len(candies)):
maxCandie = candies[i] + extraCandies
if maxCandie >= maxCandies: # this is working as expected
result[i] = True
else: result[i] = False
return result
Second that does NOT work:
class Solution(object):
def kidsWithCandies(self, candies, extraCandies):
“””
:type candies: List[int]
:type extraCandies: int
:rtype: List[bool]
“””
result = candies
#maxCandies = max(candies)
for i in range(len(candies)):
maxCandie = candies[i] + extraCandies
if maxCandie >= max(candies): # this is where it shows a boolean return
result[i] = True
else: result[i] = False
return result
As you can see, the only difference is to declare the max of Candies list before the loop.
Input of candies can be: candies = [4,2,1,1,1,2], extraCandies = 1
I tried both versions, but I have no idea why the second is different from the first
coli9cj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.