当前位置:网站首页>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())
}
}
边栏推荐
- Jetson nano custom boot icon kernel logo CBOOT logo
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
- Learning C language from scratch -- installation and configuration of 01 MinGW
- [set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
- Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
- [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 edition 13] - gapnet: graph attention based point neural network for exploring local feature
- Quickly use markdown to edit articles
- NPM install installation dependency package error reporting solution
- Win10安装ELK
猜你喜欢

Win10 quick screenshot

NPM install installation dependency package error reporting solution

Run flash demo on ECS
![[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis](/img/c6/5f723d9021cf684dcfb662ed3acaec.png)
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis

Jenkins learning (I) -- Jenkins installation
![[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks](/img/61/aa8d0067868ce9e28cadf5369cd65e.png)
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks

Hudi 数据管理和存储概述
![[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature](/img/66/2e7668cfed1ef4ddad26deed44a33a.png)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature

Flink学习笔记(八)多流转换

Jenkins learning (II) -- setting up Chinese
随机推荐
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Flink学习笔记(九)状态编程
Jetson nano custom boot icon kernel logo CBOOT logo
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
Filter comments to filter out uncommented and default values
The server denied password root remote connection access
LeetCode每日一题(1362. Closest Divisors)
Jenkins learning (III) -- setting scheduled tasks
LeetCode每日一题(2115. Find All Possible Recipes from Given Supplies)
Vscode Arduino installation Library
Computing level network notes
LeetCode每日一题(745. Prefix and Suffix Search)
The idea of compiling VBA Encyclopedia
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
Apply for domain name binding IP to open port 80 record
Overview of database system
Flink学习笔记(八)多流转换
IDEA 中使用 Hudi
Spark 概述
Win10 install elk