20250302 finished.
This commit is contained in:
parent
2dd2c8dd84
commit
df8d8adfa2
|
@ -512,3 +512,5 @@ mod p1472_design_browser_history;
|
||||||
mod p2353_design_a_food_rating_system;
|
mod p2353_design_a_food_rating_system;
|
||||||
|
|
||||||
mod p131_palindrome_partitioning;
|
mod p131_palindrome_partitioning;
|
||||||
|
|
||||||
|
mod p132_palindrome_partitioning_ii;
|
||||||
|
|
51
src/problem/p132_palindrome_partitioning_ii.rs
Normal file
51
src/problem/p132_palindrome_partitioning_ii.rs
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/**
|
||||||
|
* [132] Palindrome Partitioning II
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn min_cut(s: String) -> i32 {
|
||||||
|
let s: Vec<char> = s.chars().collect();
|
||||||
|
let length = s.len();
|
||||||
|
|
||||||
|
let mut palindorme = vec![vec![true; length]; length];
|
||||||
|
|
||||||
|
for i in (0..length).rev() {
|
||||||
|
for j in i + 1..length {
|
||||||
|
palindorme[i][j] = (s[i] == s[j]) && palindorme[i + 1][j - 1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut dp = vec![i32::MAX; length];
|
||||||
|
|
||||||
|
for i in 0..length {
|
||||||
|
if palindorme[0][i] {
|
||||||
|
dp[i] = 0;
|
||||||
|
} else {
|
||||||
|
for j in 0..i {
|
||||||
|
if palindorme[j + 1][i] {
|
||||||
|
dp[i] = dp[i].min(dp[j] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dp[length - 1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_132() {
|
||||||
|
assert_eq!(1, Solution::min_cut("aab".to_owned()));
|
||||||
|
assert_eq!(0, Solution::min_cut("a".to_owned()));
|
||||||
|
assert_eq!(1, Solution::min_cut("ab".to_owned()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user