Is it possible to do it with DP in C++?


  • 0
    J

    I exceeds time limits with DP when the inputs are very large. However the results are correct on my computer. Is it possible to do in this way, with all optimizations possible?

    class Solution {
    

    public:

    int maxArea(vector<int>& height) {
        
        vector<int> currentMax(height.size(), 0);
        //current[i] represents the maxArea with the previous i heights;
        int i, j;
        
        for(i = 1; i < height.size(); ++i){
            
            for(j = 0; j < i; ++j){
                
                if(currentMax[i] < max(currentMax[i - 1], (i - j) * min(height[i], height[j])))
                
                    currentMax[i] = max(currentMax[i - 1], (i - j) * min(height[i], height[j]));
                
            }
            
        }
        
        return currentMax[height.size() - 1];
        
    }
    

    };


Log in to reply
 

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