[Leetcode]Dynamic Programming

Posted stary_yan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode]Dynamic Programming相关的知识,希望对你有一定的参考价值。

Dynamic Programming

In this blog, I will solve two problem in leetcode to introduce an important idea in dynamic programming. As we all know, dynamic programming is one of the hardest algorithms to understand but there are some useful ideas. Let’s start.

[139] Word Break

Description

The problem is easy to understand. We need to use the words in dict to construct the string s. The straightforward solution is brutal force by constructing all of the possible string by using words in dict. But the time complexity is about (n!) which can’t be accepted. In dynamic programming, the general framework of algorithm is to find a equation, dp[i] = dp[j] + dp[i -j]. So we can loop the string s and construct a vector dp and dp[i] means the string which ends in i index of s can be constructed by using words in dict. So the equation will become dp[i] = dp[j] + s[j : i], if dp[j] is true and s[j : i] is a word in dict, then dp[i] will be true.

Code

class Solution 
public:
    bool wordBreak(string s, vector<string>& wordDict) 
        vector<bool> tables(s.size() + 1, false);
        tables[0] = true
        for (int i = 1; i <= s.size(); i++) 
            for (int j = i - 1; j >= 0; j--) 
                if (tables[j]) 
                    string word = s.substr(j, i - j);
                    if (wordDict.find(word) != wordDict.end()) 
                        tables[i] = true;
                        break;
                    
                
            
        
        return tables[s.size()];
    
;

Time complexity: O( n^2 * s )
Space complexity: O( n )

[140]word-break-ii

Description

The question is a bit harder than the question which is mentioned above. The idea is the same, and you have to store the prev index of dp[i] which can be used to backtracking.

class Solution 
private:
    void generate(vector<string> &answer, vector<vector<int>>& tables, int cur, string str, string& s) 
        if (cur == 0) 
            answer.push_back(str.substr(1, str.size() - 1));
         else 
            for (int i = 0; i < tables[cur].size(); i++) 
                string ano = " " + s.substr(tables[cur][i], cur - tables[cur][i]) + str;
                generate(answer, tables, tables[cur][i], ano, s);
            
        
    
public:
    vector<string> wordBreak(string s, vector<string>& wordDict) 
        vector<vector<int>> tables(s.size() + 1, vector<int>());
        tables[0].push_back(0);
        for (int i = 1; i <= s.size(); i++) 
            for (int j = i - 1; j >= 0; j--) 
                if (tables[j].size() != 0) 
                    string word = s.substr(j, i - j);
                    if (find(wordDict.begin(), wordDict.end(), word) != wordDict.end()) 
                        tables[i].push_back(j);
                    
                
            
        
        vector<string> answer;
        string str = "";
        generate(answer, tables, s.size(), str, s);
        return answer;
    
;

以上是关于[Leetcode]Dynamic Programming的主要内容,如果未能解决你的问题,请参考以下文章

[Leetcode]Dynamic Programming

[Leetcode]Dynamic Programming

[Leetcode]Dynamic Programming

Dynamic Programming - leetcode [动态规划]

leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)

leetcode@ [343] Integer Break (Math & Dynamic Programming)