当前位置:网站首页>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())
}
}
边栏推荐
- LeetCode每日一题(1162. As Far from Land as Possible)
- 制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
- PIP configuring domestic sources
- Utilisation de hudi dans idea
- [solution to the new version of Flink without bat startup file]
- Crawler career from scratch (I): crawl the photos of my little sister ① (the website has been disabled)
- Run flash demo on ECS
- LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
- Overview of image restoration methods -- paper notes
- [kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions
猜你喜欢

There is no open in default browser option in the right click of the vscade editor

LeetCode每日一题(931. Minimum Falling Path Sum)

Hudi学习笔记(三) 核心概念剖析
![[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
![[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?](/img/fc/5c71e6457b836be04583365edbe08d.png)
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?

Apply for domain name binding IP to open port 80 record

Trial of the combination of RDS and crawler

Jenkins learning (III) -- setting scheduled tasks

Hudi integrated spark data analysis example (including code flow and test results)

Win10安装ELK
随机推荐
图像修复方法研究综述----论文笔记
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
Install database -linux-5.7
Run flash demo on ECS
Just graduate student reading thesis
Linxu learning (4) -- Yum and apt commands
Jestson Nano 从tftp服务器下载更新kernel和dtb
MySQL installation and configuration (command line version)
Detailed steps of windows installation redis
Liteide is easy to use
Principles of computer composition - cache, connection mapping, learning experience
Vscode Arduino installation Library
Integrated use of interlij idea and sonarqube
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Jetson Nano 自定义启动图标kernel Logo cboot logo
C language programming specification
IDEA 中使用 Hudi
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
Vscode编辑器右键没有Open In Default Browser选项