[cut head or tail]Golang version


  • 0
    S

    Hello there.What I do is just compare the two lists .
    first list is the list move the first element
    and the second list is a last element-moved list

    import (
    	"fmt"
    	"math"
    )
    
    func rob(nums []int) int {
        var sum int
        length:=len(nums)
        if length==0{
        	sum=0
        	}else if length==1{
        		sum=nums[0]
        		}else{
        		//round1
        res:=make([]int,length+1)
        res[0]=0
        res[1]=nums[0]
        for i:=2;i<length+1;i++{
        	res[i]=int(math.Max(float64(nums[i-1]+res[i-2]),float64(res[i-1])))
        	}
        sum=res[length-1]
        //round2
        len2:=length-1;   
         res2:=make([]int,length+1)
         res2[0]=0
         res2[1]=nums[1]   
          for i:=2;i<len2+1;i++{
        	res2[i]=int(math.Max(float64(nums[i]+res2[i-2]),float64(res2[i-1])))
        	}
        sum2:=res2[len2]  
        if sum2>=sum{
        	sum=sum2
        	}
        }
        	    	
        return sum
    }

Log in to reply
 

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