LeetCode 229场周赛 - 区间DP + 最长公共子序列

1770. Maximum Score from Performing Multiplication Operations

You are given two integer arrays nums and multipliers of size n and m respectively, where n >= m. The arrays are 1-indexed.

You begin with a score of 0. You want to perform exactly m operations. On the ith operation (1-indexed), you will:

  • Choose one integer x from either the start or the end of the array nums.
  • Add multipliers[i] * x to your score.
  • Remove x from the array nums.

Return the maximum score after performing m operations.

Example 1:

Input: nums = [1,2,3], multipliers = [3,2,1]
Output: 14
Explanation: An optimal solution is as follows:
- Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score.
- Choose from the end, [1,2], adding 2 * 2 = 4 to the score.
- Choose from the end, [1], adding 1 * 1 = 1 to the score.
The total score is 9 + 4 + 1 = 14.

Example 2:

Input: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
Output: 102
Explanation: An optimal solution is as follows:
- Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score.
- Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score.
- Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score.
- Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score.
- Choose from the end, [-2,7], adding 7 * 6 = 42 to the score. 
The total score is 50 + 15 - 9 + 4 + 42 = 102.

 

Constraints:

  • n == nums.length
  • m == multipliers.length
  • 1 <= m <= 103
  • m <= n <= 105
  • -1000 <= nums[i], multipliers[i] <= 1000

LeetCode 229场周赛 - 区间DP + 最长公共子序列_第1张图片

时间复杂度: O(m^2)

    public int maximumScore(int[] a, int[] w) {
        int n = a.length, m = w.length;
        if(n > 2 * m){
            int idx = m, y= n - m;
            while(y < n) a[idx++] = a[y++];
            n = idx;
        }
        int[][] f = new int[n + 2][n + 2];
        for(int len = n - m + 1; len <= n; len++){
            for(int i = 1; i + len - 1 <= n; i++){
                int j = i + len - 1;
                f[i][j] = Math.max(f[i + 1][j] + a[i - 1] * w[n - len], f[i][j - 1] + a[j - 1] * w[n - len]);
            }
        }
        return f[1][n];
    }

 

 

1771. Maximize Palindrome Length From Subsequences

My SubmissionsBack to Contest

  • User Accepted:557
  • User Tried:1484
  • Total Accepted:609
  • Total Submissions:3692
  • Difficulty:Hard

You are given two strings, word1 and word2. You want to construct a string in the following manner:

  • Choose some non-empty subsequence subsequence1 from word1.
  • Choose some non-empty subsequence subsequence2 from word2.
  • Concatenate the subsequences: subsequence1 + subsequence2, to make the string.

Return the length of the longest palindrome that can be constructed in the described manner. If no palindromes can be constructed, return 0.

subsequence of a string s is a string that can be made by deleting some (possibly none) characters from s without changing the order of the remaining characters.

palindrome is a string that reads the same forward as well as backward.

 

Example 1:

Input: word1 = "cacb", word2 = "cbba"
Output: 5
Explanation: Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.

Example 2:

Input: word1 = "ab", word2 = "ab"
Output: 3
Explanation: Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.

Example 3:

Input: word1 = "aa", word2 = "bb"
Output: 0
Explanation: You cannot construct a palindrome from the described method, so return 0.

 

Constraints:

  • 1 <= word1.length, word2.length <= 1000
  • word1 and word2 consist of lowercase English letters.

LeetCode 229场周赛 - 区间DP + 最长公共子序列_第2张图片

 

时间复杂度: O(max(n * m, n * n, m * m))

class Solution {
    int[][] fc, f1, f2;
    
    public void maxPalindrome(int[][] f, String s){
        int n = s.length() - 1;
        for(int len = 1; len <= n; len++){
            for(int i = 1; i + len - 1 <= n; i++){
                int j = i + len - 1;
                if(len == 1) f[i][j] = 1;
                else{
                    f[i][j] = Math.max(f[i + 1][j], f[i][j - 1]);
                    if(s.charAt(i) == s.charAt(j)) f[i][j] = Math.max(f[i][j], f[i + 1][j - 1] + 2);
                }
            }
        }
    }
    
    public int longestPalindrome(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        fc = new int[n + 1][m + 1]; f1 = new int[n + 2][n + 2]; f2 = new int[m + 2][m + 2];
        s1 = " " + s1;
        s2 = " " + (new StringBuilder(s2).reverse().toString());
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                fc[i][j] = Math.max(fc[i - 1][j], fc[i][j - 1]);
                if(s1.charAt(i) == s2.charAt(j)) fc[i][j] = Math.max(fc[i][j], fc[i - 1][j - 1] + 1);
            }
        }
        
        maxPalindrome(f1, s1); maxPalindrome(f2, s2);
        
        int ans = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                ans = Math.max(fc[i][j] * 2 +
                              (fc[i][j] != 0 ? Math.max(f1[i + 1][n], f2[j + 1][m]) : 0 ), ans);
            }
        }
        return ans;
    }
}

 

 

 

你可能感兴趣的:(算法与数据结构,算法,algorithm)