当前位置:网站首页>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())
}
}
边栏推荐
- The idea of compiling VBA Encyclopedia
- Powerdesign reverse wizard such as SQL and generates name and comment
- Go language - IO project
- LeetCode每日一题(1162. As Far from Land as Possible)
- CATIA automation object architecture - detailed explanation of application objects (III) systemservice
- Just graduate student reading thesis
- 图像修复方法研究综述----论文笔记
- Hudi学习笔记(三) 核心概念剖析
- Hudi 数据管理和存储概述
- LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
猜你喜欢

Go language - Reflection

Arduino handles JSON data, arduinojson assistant

npm install安装依赖包报错解决方法

文件系统中的目录与切换操作

Hudi 数据管理和存储概述

Vscode编辑器右键没有Open In Default Browser选项

Jenkins learning (I) -- Jenkins installation

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

Principles of computer composition - cache, connection mapping, learning experience
![[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu](/img/57/b413a93a456a1872fc19aa825c937a.jpg)
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
随机推荐
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
NPM install installation dependency package error reporting solution
Overview of database system
Jenkins learning (I) -- Jenkins installation
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
PolyWorks script development learning notes (III) -treeview advanced operation
Hudi integrated spark data analysis example (including code flow and test results)
Jenkins learning (II) -- setting up Chinese
Call the contents of Excel cells opened at the same time - button line feed
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
About the configuration of vs2008+rade CATIA v5r22
Hudi learning notes (III) analysis of core concepts
Please tell me how to set vscode
MySQL installation and configuration (command line version)
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
[solution to the new version of Flink without bat startup file]
Alibaba cloud notes for the first time
一款开源的Markdown转富文本编辑器的实现原理剖析
PolyWorks script development learning notes (II) -treeview basic operations