[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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值