Problem solution · Java

Minimum Changes to Make K Semi-palindromes

Minimum Changes to Make K Semi-palindromes: a Java solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Dynamic programming
Source
walkccc LeetCode Solutions
Length
61 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Minimum Changes to Make K Semi-palindromes, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 61 lines of Java from the credited upstream file 2911.java.
  • The implementation visibly relies on sequence storage, cached states.
  • 11 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMinimum Changes to Make K Semi-palindromes · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public int minimumChanges(String s, int k) {    final int n = s.length();    // factors[i] := factors of i    List<Integer>[] factors = getFactors(n);    // cost[i][j] := changes to make s[i..j] a semi-palindrome    int[][] cost = getCost(s, n, factors);    // dp[i][j] := the minimum changes to split s[i:] into j valid parts    int[][] dp = new int[n + 1][k + 1];     Arrays.stream(dp).forEach(A -> Arrays.fill(A, n));    dp[n][0] = 0;     for (int i = n - 1; i >= 0; --i)      for (int j = 1; j <= k; ++j)        for (int l = i + 1; l < n; ++l)          dp[i][j] = Math.min(dp[i][j], dp[l + 1][j - 1] + cost[i][l]);     return dp[0][k];  }   private List<Integer>[] getFactors(int n) {    List<Integer>[] factors = new List[n + 1];    for (int i = 1; i <= n; ++i)      factors[i] = new ArrayList<>(List.of(1));    for (int d = 2; d < n; ++d)      for (int i = d * 2; i <= n; i += d)        factors[i].add(d);    return factors;  }   private int[][] getCost(final String s, int n, List<Integer>[] factors) {    int[][] cost = new int[n][n];    for (int i = 0; i < n; ++i)      for (int j = i + 1; j < n; ++j) {        final int length = j - i + 1;        int minCost = length;        for (final int d : factors[length])          minCost = Math.min(minCost, getCost(s, i, j, d));        cost[i][j] = minCost;      }    return cost;  }   // Returns the cost to make s[i..j] a semi-palindrome of `d`.  private int getCost(final String s, int i, int j, int d) {    int cost = 0;    for (int offset = 0; offset < d; ++offset) {      int l = i + offset;      int r = j - d + 1 + offset;      while (l < r) {        if (s.charAt(l) != s.charAt(r))          ++cost;        l += d;        r -= d;      }    }    return cost;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗