动态规划----各种最长序列

最大连续子数组和

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

click to show more practice.

More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

int maxSubArray(vector<int>& nums) {
    if (nums.size() == 0)return 0;
    int sum = nums[0];
    int maxsum = sum;
    for (int i = 1; i < nums.size(); i++){
        sum = max(nums[i], nums[i] + sum);
        maxsum = max(maxsum, sum);
    }
    return maxsum;
}
int maxSubArray(vector<int>& nums) {
    if (nums.size() == 0)return 0;
    int sum = nums[0];
    int maxsum = sum;

    for (int i = 1; i < nums.size(); i++){
        sum = sum < 0 ? nums[i] : sum + nums[i];
        maxsum = max(sum, maxsum);
    }
    return maxsum;
}

最长上升子序列

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

int lengthOfLIS(vector<int>& nums) {
    if (nums.size() == 0)return 0;
    vector<int> dp(nums.size(), 0);
    dp[0] = nums[0];
    int len = 1;
    for (int i = 1; i < nums.size(); i++){
        int idx = lower_bound(dp.begin(), dp.begin() + len, nums[i]) - dp.begin();
        if (idx == len)len++;
        dp[idx] = nums[i];
    }
    return len;
}

最长公共子序列

给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。

样例

给出”ABCD” 和 “EDCA”,这个LCS是 “A” (或 D或C),返回1
给出 “ABCD” 和 “EACB”,这个LCS是”AC”返回 2

int longestCommonSubsequence(string A, string B) {
    if (A == "" || B == "")return 0;
    vector<vector<int>> dp(A.size() + 1, vector<int>(B.size() + 1));
    int len1 = A.size(), len2 = B.size();
    for (int i = 1; i <= A.size(); i++){
        for (int j = 1; j <= B.size(); j++){
            if (A[i - 1] == B[j - 1])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            else
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }
    return dp[len1][len2];
}

编辑距离

给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数。

你总共三种操作方法:

插入一个字符
删除一个字符
替换一个字符

样例
给出 work1=”mart” 和 work2=”karma”

返回 3

int minDistance(string word1, string word2) {
    int len1 = word1.size(), len2 = word2.size();
    vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.size() + 1));

    for (int i = 1; i <= len1; i++)dp[i][0] = i;
    for (int j = 1; j <= len2; j++)dp[0][j] = j;

    for (int i = 1; i <= len1; i++){
        for (int j = 1; j <= len2; j++){
            if (word1[i - 1] == word2[j - 1])dp[i][j] = dp[i - 1][j - 1];
            else dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
        }
    }
    return dp[len1][len2];
}

最长回文子串

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: "babad"

Output: "bab"

Note: “aba” is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

针对某个当前点,以当前点为中心,找出最长的回文子串;

string Palsubstr(string& str, int low, int high){
    while (low >= 0 && high < str.size() && str[low] == str[high]){
        low--, high++;
    }
    return str.substr(low + 1, high - low - 1);
}

string longestPalindrome(string s) {
    if (s == "")return "";
    int center = 0, max_len = 0;
    string maxstr = s.substr(0, 1);
    for (int i = 0; i < s.size(); i++){
        string str1 = Palsubstr(s, i, i);
        string str2 = Palsubstr(s, i, i + 1);
        int len1 = str1.size(), len2 = str2.size();
        if (max(len1, len2) > maxstr.size()){
            if (len1 > len2)maxstr = str1;
            else maxstr = str2;
        }
    }
    return maxstr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值