My 4 lines python


  • 0
    Z

    here is my code: i am using the state switching solution.
    for current house: there are 2 states, rob and not rob.

    so the rob --> not rob (obviously because you can not rob next house)
    not rob--> rob (rob the current one, then the state changed)
    not rob --> not rob(do nothing)

    rob = not_rob = 0
    for n in nums:
    rob,not_rob = not_rob +n,max(not_rob,rob)
    return max(rob,not_rob)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.