C++ 5 lines DP


  • 0
    V
    int longestPalindromeSubseq(string s) {
        int dp[s.size() + 1][s.size() + 1] = {};
        for (int len = 1; len <= s.size(); ++len) 
            for (auto i = 0; i + len <= s.size(); ++i) dp[i][i + len] = s[i] == s[i + len - 1] ? 
                dp[i + 1][i + len - 1] + (len == 1 ? 1 : 2) : max(dp[i][i + len - 1],  dp[i + 1][i + len]);
        return dp[0][s.size()];
    }
    

Log in to reply
 

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