less than 1 minute read

3259. Maximum Energy Boost From Two Drinks

Solution

class Solution {
    int[] energyDrinkA;
    int[] energyDrinkB;
    
    public long maxEnergyBoost(int[] energyDrinkA, int[] energyDrinkB) {
        this.energyDrinkA = energyDrinkA;
        this.energyDrinkB = energyDrinkB;

        long[][] dp = new long[energyDrinkA.length][2];
        for(long[] row: dp) {
            Arrays.fill(row, -1);
        }

        return Math.max(process(0, 0, dp), process(0, 1, dp));
    }

    private long process(int i, int cur, long[][] dp) {
        if (i >= dp.length) {
            return 0;
        }

        if(dp[i][cur] != -1) {
            return dp[i][cur];
        }

        long takeCur = 0;
        if (cur == 1) { 
            takeCur = process(i + 1, cur, dp) + energyDrinkA[i];
        } else {
            takeCur = process(i + 1, cur, dp) + energyDrinkB[i];
        }

        long switchVal = process(i + 1, 1 - cur, dp);

        long res = Math.max(takeCur, switchVal);
        dp[i][cur] = res;

        return res;
    }
}

Retro

  1. didn’t figure out this is a recursion solution…