题目1:LintCode 108 Palindrome Partitioning II
题目2:LintCode 108 Palindrome Partitioning II
Input: "aab"
Output: 1
Explanation: Split "aab" once, into "aa" and "b", both palindrome.
Each section of the string is divided into several strings happened division happened, that is the longest string palindrome
Determine the status:
Last paragraph palindromic sequence S [j..N-1] need to know prior to S j string S [0..j-1] can be divided into several strings happened
dp [i]: Let S happened before the i-th character string of love can be divided into several
Finally, a palindromic sequence length is at least 1, for all j from 0 to i-1
Assumptions: The last was a palindromic sequence
dp [i] = min {dp [j] +1} | S [j..i-1] palindromic sequence interval [0,. 1, 2, J-I. 1 ... , I]
j=0..i-1
dp [0] = 0; empty string may be divided into a palindromic sequence 0
Analyzing how palindromic sequence:
- Odd outer axis, both sides are the same
- Both sides are the same even number
In the center of each substring extended to both sides of the time complexity of 0 (n ^ 2)
With isPalin [i] [j]: indicates if s [i, j] palindromic sequence
dp [i] = min {dp [j] +1} | S [j..i-1] palindromic sequence ====> dp [i] = min {dp [j] +1} | isPalin [ i] [j] = True
The answer is F [N] -1 happened several divided F [N]: N is represented with a minimum number of palindromic sequence ends.
Time complexity 0 (n ^ 2) spatial complexity 0 (n ^ 2)
1 public class Solution { 2 /** 3 * @param s: A string 4 * @return: An integer 5 */ 6 public int minCut(String ss) { 7 // write your code here 8 char[] s = ss.toCharArray(); 9 int n = s.length; 10 if(n==0){ 11 return 0; 12 } 13 boolean[][] isPalin = new boolean[n][n]; 14 for(int i=0; i<n; i++){ 15 for(int j=0; j<n; j++){ 16 isPalin[i][j] = false; 17 } 18 } 19 int i, j, t; 20 // 判断i到j是否为回文串 21 for(t=0; t<n; t++){ 22 // old-length 23 i=j=t; 24 while(i>=0 && j<n && s[i]==s[j]){ 25 isPalin[i][j] = true; 26 i--; 27 j++; 28 } 29 30 // even length 31 i = t; 32 j = t+1; 33 while(i>=0 && j<n && s[i]==s[j]){ 34 isPalin[i][j] = true; 35 i--; 36 j++; 37 } 38 } 39 40 int[] dp = new int[n+1]; 41 dp[0]=0; 42 for(i=1; i<=n; i++){ 43 dp[i] = Integer.MAX_VALUE; 44 for(j=0; j<i; j++){ 45 if(isPalin[j][i-1]){ 46 dp[i] = Math.min(dp[j]+1, dp[i]); 47 } 48 } 49 } 50 51 // dp[i] = min j=0..i-1 {f[j]+1 | isPalin[j][i-1] = True} 52 53 return dp[n]-1; 54 } 55 }
Topic 3: LintCode 437 Copy Books
K a scribe, you can transcribe several consecutive present (each scribe broken every one of the same)
Q. How much time can transcription requires a minimum finish
Scribe Scribe i-j according to the second present time required: A [i] + A [i + 1] + ... + A [j]
Get one division manner, not more than k segments, each segment such that the sum of all the numbers of the maximum value of minimum
Final step: A [j] + .. + A [N-1]
We need to know how much time individual money k-1 requires a minimum of j before copying finished the book (0-j-1 book)
f [k] [i]: k of scribe minimum time required before copied the book i (k plus side, if it represents the number of the plurality of scribe is arbitrary, recording state by k)
f [k] [i] = min {max {f [k-1] [j], A [j] + A [j + 1] + ... + A [i-1]}}
j = 0, ..., j
Initial conditions:
- f[0][0]=0,
- f [0] [1] = f [0] [2] = ... = f [0] [N] = + infinity representatives can not copy, to the minimum
- f[k][0]=0;
1 public class Solution { 2 /** 3 * @param pages: an array of integers 4 * @param k: An integer 5 * @return: an integer 6 */ 7 public int copyBooks(int[] A, int K) { 8 // write your code here 9 int n = A.length; 10 if(n==0){ 11 return 0; 12 } 13 if(K > n){ 14 K = n; 15 } 16 17 int[][] dp = new int[K+1][n+1]; 18 int i, j, k; 19 20 dp[0][0] = 0; 21 for(j=1; j<=n; j++){ 22 dp[0][j] = Integer.MAX_VALUE; 23 } 24 25 int sum = 0; 26 for(k=1; k<=K; k++){ 27 dp[k][0] = 0; 28 for(i=1; i<=n; i++){ 29 sum = 0; 30 dp[k][i] = Integer.MAX_VALUE; 31 //import j 是从j到i-1 32 for(j=i; j>=0; j--){ 33 dp[k][i] = Math.min(dp[k][i], Math.max(dp[k-1][j], sum)); 34 if(j > 0){ 35 sum += A[j-1]; 36 } 37 } 38 } 39 // f[k][j] = min j=0..i {max{f[k-1][j], A[j]+...+A[i-1]}} 40 } 41 42 return dp[K][n]; 43 } 44 }