Leetcode 1326. The minimum number of faucets to irrigate the garden (DAY 69) ---- Dynamic programming learning period (the algorithm problem path is so long and long, it does accumulate too little)

Original title

Insert picture description here


Code implementation (most of the first brush to see the solution, a small part of self-solving)

class Solution {
    
    
public:
    int minTaps(int n, vector<int>& ranges) {
    
    
        vector<int> dp(n+1,INT_MAX);
        int l,r,pre;
        for(int i=0;i<=n;++i)
        {
    
    
            if(!ranges[i])  continue;
            l = max(0,i-ranges[i]);
            r = min(n,i+ranges[i]);

            pre = (l<=0 ? 0: dp[l]);
            if(pre == INT_MAX) continue;
            for(int temp = l;temp<=r;temp++)
                dp[temp] = min(dp[temp],pre+1);
        }
        return dp[n]==INT_MAX ? -1:dp[n];
    }
};

Guess you like

Origin blog.csdn.net/qq_37500516/article/details/115205754