Leetcode 646. Maximum Length of Pair Chain

Problem

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.

Algorithm

Dynamic Programming (DP). Sort the data with a and b, define the state f(i) is the longest palindromic subsequence's length, then
f ( i ) = m a x { f ( j ) + 1 } , i f s o r t e d _ p a i r s i 0 > s o r t e d _ p a i r s j 1 f(i) = max\{ f(j) + 1 \}, \quad if \quad sorted\_pairsi0 > sorted\_pairsj1 f(i)=max{f(j)+1},ifsorted_pairsi0>sorted_pairsj1

Code

python3 复制代码
class Solution:
    def findLongestChain(self, pairs: List[List[int]]) -> int:
        plen = len(pairs)
        dp = [1] * plen
        sorted_pairs = sorted(pairs, key=lambda x: (x[0], x[1]))
        for i in range(1, plen):
            for j in range(i):
                if sorted_pairs[i][0] > sorted_pairs[j][1] and dp[i] <= dp[j]:
                     dp[i] = dp[j] + 1
        
        return max(dp)
相关推荐
吞下星星的少年·-·2 分钟前
Div.2 CF 1130B题(构造,贪心)
算法·贪心·构造
文丑颜不良啊5 分钟前
LeetCode1481~1500题目解析
java·leetcode
文丑颜不良啊1 小时前
LeetCode1381~1400题目解析
java·leetcode
是main不是漫1 小时前
【双指针——移动零】数组划分类题目的入门基石
c++·算法
深圳市快瞳科技有限公司2 小时前
野外鸟类AI识别落地实录:输入适配、结构化输出、去重聚合与置信度分流的工程实践
算法·计算机视觉·鸟类识别·鸟类ai识别·野外鸟类观测·林业调查
专注仿真4 小时前
梯度提升类(XFBoost)算法详解与代码实例
开发语言·人工智能·python·算法·大模型训练·控制算法
小豆庄4 小时前
视频遮罩追踪退场后ID撞号的排查—多目标场景下的失效
opencv·算法·bug·视频·python3.11
Tisfy4 小时前
LeetCode 3882.网格图中最小异或路径:动态规划
算法·leetcode·动态规划
D e re k ZH5 小时前
P5025 [SNOI2017] 炸弹
c++·算法
A boy CDEF girl6 小时前
算法题专栏:优选算法
数据结构·算法·leetcode