LeetCode //C - 1278. Palindrome Partitioning III

1278. Palindrome Partitioning III

You are given a string s containing lowercase letters and an integer k. You need to :

  • First, change some characters of s to other lowercase English letters.
  • Then divide s into k non-empty disjoint substrings such that each substring is a palindrome.

Return the minimal number of characters that you need to change to divide the string.

Example 1:

Input: s = "abc", k = 2

Output: 1

Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.

Example 2:

Input: s = "aabbc", k = 3

Output: 0

Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome.

Example 3:

Input: s = "leetcode", k = 8

Output: 0

Constraints:
  • 1 <= k <= s.length <= 100.
  • s only contains lowercase English letters.

From: LeetCode

Link: 1278. Palindrome Partitioning III


Solution:

Ideas:

Precompute the cost to convert every substring into a palindrome, then use dynamic programming to find the minimum cost of splitting the string into exactly k parts.

Code:
c 复制代码
#include <string.h>
#include <limits.h>

int palindromePartition(char* s, int k) {
    int n = strlen(s);

    // cost[i][j] = minimum changes needed to make s[i..j] a palindrome
    int cost[100][100] = {0};

    for (int length = 2; length <= n; length++) {
        for (int i = 0; i + length <= n; i++) {
            int j = i + length - 1;

            cost[i][j] = cost[i + 1][j - 1];

            if (s[i] != s[j]) {
                cost[i][j]++;
            }
        }
    }

    // dp[parts][i] = minimum changes to split first i characters
    // into exactly parts palindromic substrings
    int dp[101][101];

    for (int parts = 0; parts <= k; parts++) {
        for (int i = 0; i <= n; i++) {
            dp[parts][i] = INT_MAX / 2;
        }
    }

    dp[0][0] = 0;

    for (int parts = 1; parts <= k; parts++) {
        for (int i = parts; i <= n; i++) {
            for (int split = parts - 1; split < i; split++) {
                int current =
                    dp[parts - 1][split] + cost[split][i - 1];

                if (current < dp[parts][i]) {
                    dp[parts][i] = current;
                }
            }
        }
    }

    return dp[k][n];
}
相关推荐
凌冰_5 分钟前
数据结构 - 矩阵(九)
算法·矩阵
Regulus-Moon11 分钟前
凸优化:局部即全局的定价表——条件数 κ 上的速率谱系
算法·机器学习·数学建模
FPGA小迷弟18 分钟前
PID 算法全解:从历史渊源到 FPGA 硬件实现
算法·fpga开发·硬件架构·fpga·sm4
洞见新研社19 分钟前
半年从蓝海杀入红海,机器人数据生意变天了
人工智能·算法·机器人
纪念 22943 分钟前
C++ 算法模板与错题 (高精度)
开发语言·c++·算法
烟烟Abandon1 小时前
FFmpeg开发基础之 C 语言中的指针(下篇)
c语言·ffmpeg
Navigator_Z2 小时前
LeetCode //C - 1331. Rank Transform of an Array
c语言·算法·leetcode
Tisfy2 小时前
LeetCode 1202.交换字符串中的元素:分组排序(连通图)
算法·leetcode·字符串·bfs·题解·排序·连通图
csdn_aspnet9 小时前
C++ 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)
c++·算法