当前位置:网站首页>Leetcode daily question (516. long palindromic subsequence)
Leetcode daily question (516. long palindromic subsequence)
2022-07-03 09:33:00 【wangjun861205】
Given a string s, find the longest palindromic subsequence’s length in s.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: s = “bbbab”
Output: 4
Explanation: One possible longest palindromic subsequence is “bbbb”.
Example 2:
Input: s = “cbbd”
Output: 2
Explanation: One possible longest palindromic subsequence is “bb”.
Constraints:
- 1 <= s.length <= 1000
- s consists only of lowercase English letters.
dp[i][j] representative s[i…=j] Length of palindrome in , When we find a pair of the same characters s[i] == s[k], If we want to put this pair of characters in the palindrome , The final palindrome length is dp[i+1][k-1] + 2, If we want to skip this pair of characters , It is dp[i+1][j]
use std::collections::HashMap;
impl Solution {
fn dp(chars: &Vec<char>, i: usize, j: usize, cache: &mut HashMap<(usize, usize), i32>) -> i32 {
if i > j {
return 0;
}
if i == j {
return 1;
}
let mut next_j = j;
while i < next_j {
if chars[i] != chars[next_j] {
next_j -= 1;
continue;
}
break;
}
let pick = if i == next_j {
1
} else {
if let Some(c) = cache.get(&(i + 1, next_j - 1)) {
*c + 2
} else {
Solution::dp(chars, i + 1, next_j - 1, cache) + 2
}
};
let pass = if let Some(c) = cache.get(&(i + 1, j)) {
*c
} else {
Solution::dp(chars, i + 1, j, cache)
};
let ans = pick.max(pass);
cache.insert((i, j), ans);
ans
}
pub fn longest_palindrome_subseq(s: String) -> i32 {
let chars: Vec<char> = s.chars().collect();
Solution::dp(&chars, 0, chars.len() - 1, &mut HashMap::new())
}
}
边栏推荐
- Jenkins learning (I) -- Jenkins installation
- IDEA 中使用 Hudi
- Please tell me how to set vscode
- Powerdesign reverse wizard such as SQL and generates name and comment
- 小王叔叔的博客目录【持续更新中】
- Construction and test of TFTP server under unbuntu (Debian)
- Spark cluster installation and deployment
- Run flash demo on ECS
- Spark 集群安装与部署
- Desktop icon recognition based on OpenCV
猜你喜欢

Crawler career from scratch (IV): climb the bullet curtain of station B through API

Run flash demo on ECS

PolyWorks script development learning notes (II) -treeview basic operations

2022-2-14 learning the imitation Niuke project - send email

Hudi quick experience (including detailed operation steps and screenshots)

Win10 quick screenshot

Idea uses the MVN command to package and report an error, which is not available

Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)

Navicat, MySQL export Er graph, er graph

npm install安装依赖包报错解决方法
随机推荐
Idea uses the MVN command to package and report an error, which is not available
LeetCode每日一题(1362. Closest Divisors)
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
Modify idea code
Utilisation de hudi dans idea
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
Long类型的相等判断
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
2022-2-14 learning xiangniuke project - Session Management
全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
LeetCode每日一题(2305. Fair Distribution of Cookies)
Quickly use markdown to edit articles
解决Editor.md上传图片获取不到图片地址问题
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
LeetCode每日一题(968. Binary Tree Cameras)
Learning C language from scratch -- installation and configuration of 01 MinGW
The rise and fall of mobile phones in my perspective these 10 years
Beego learning - JWT realizes user login and registration
Spark 概述