Solutions for To solve math problem

    • @DarrenNop
    • @william, submitted in JavaScript, achived 100% score
    function solve(arr) {
        let dp = [];
        let len = arr.length;
        while (len > 0) { 
            dp.push(0);
            len--;
        }
        dp[0] = arr[0];
        dp[1] = Math.max(arr[1], dp[0])
        for (let i = 2; i < arr.length; i++) { 
            dp[i] = Math.max(arr[i], dp[i - 1], arr[i] + dp[i - 2])
        }
        return dp[arr.length-1]    
    }
    • @DarrenNop
    • @william, submitted in Python 2, achived 100% score
    def solve(arr):
        dp = {} 
        dp[0], dp[1] = arr[0], max(arr[0], arr[1])
        for i, num in enumerate(arr[2:], start=2):
            dp[i] = max(dp[i-1], dp[i-2]+num, dp[i-2], num)
        return dp[len(arr)-1]
    
    • @DarrenNop
    • @william, submitted in Python 3, achived 100% score
    def solve(arr):
        dp = {} 
        dp[0], dp[1] = arr[0], max(arr[0], arr[1])
        for i, num in enumerate(arr[2:], start=2):
            dp[i] = max(dp[i-1], dp[i-2]+num, dp[i-2], num)
        return dp[len(arr)-1]