当前位置:网站首页>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())
}
}
边栏推荐
- 用Redis实现分布式锁
- Detailed steps of windows installation redis
- 全球KYC服务商ADVANCE.AI 活体检测产品通过ISO国际安全认证 产品能力再上一新台阶
- Spark 集群安装与部署
- Solve POM in idea Comment top line problem in XML file
- 数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
- The rise and fall of mobile phones in my perspective these 10 years
- Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
- LeetCode每日一题(2109. Adding Spaces to a String)
- Hudi 数据管理和存储概述
猜你喜欢
Computing level network notes
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Trial of the combination of RDS and crawler
Go language - Reflection
Spark cluster installation and deployment
Spark overview
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Apply for domain name binding IP to open port 80 record
随机推荐
QT qstring:: number apply base conversion
Usage of pandas to obtain MySQL data
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
Hudi data management and storage overview
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
Quickly use markdown to edit articles
Basic knowledge of database design
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Go language - IO project
PolyWorks script development learning notes (III) -treeview advanced operation
Matlab dichotomy to find the optimal solution
Win10 quick screenshot
[untitled] use of cmake
There is no open in default browser option in the right click of the vscade editor
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
2022-2-13 learning the imitation Niuke project - home page of the development community
Powerdesign reverse wizard such as SQL and generates name and comment
LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
Logstash+jdbc data synchronization +head display problems
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks