Can Someone Explain what is happening on line 4. I mean how the value of 'now' is getting updated?

Viewed 36

this is the code a question which uses greedy approach, can someone help what is happening at line 5 i.e., how the value of 'now' getting updated?

def calculate():
    arr=[30, 15, 60, 75, 45, 15, 15, 45]
    last,now=0,0
    for i in arr:
        last,now=now,max(last+i,now)
    return now
1 Answers

The line is basically the same as:

temp = last
last = now
now = max(temp+i, now)
Related