Maximum Energy Boost From Two Drinks - DP [JS]
Description Solution: DP Keep track of four states: prevPrevA : The max energy ending with drink A, at the previous previous index (needed when we switch to another drink type). prevPrevB : The max energy ending with drink B, at the previous previous index (needed when we switch to another drink type). prevA : The max energy ending with drink A, at the previous index. prevB : The max energy ending with drink B, at the previous index. For each index i , we can either Stay with the same drink type and take the energy at the previous energy ending with the same type. Switch the drink type and take the energy at the previous previous energy ending with the other type. Update those four states as we go through each index. Time Complexity: O(n) Space Complexity: O(1) function maxEnergyBoost ( energyDrinkA, energyDrinkB ) { let prevPrevA = 0 , prevPrevB = 0 ; let prevA = 0 , prevB = 0 ; let n = energyDrinkA.length; for ( let i = 0 ; i < n; i++) { l...