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)
相关推荐
Frostnova丶2 小时前
【算法笔记】数学知识
笔记·算法
吴可可1233 小时前
AutoCAD 2016与2014二次开发关键差异
算法
雨白4 小时前
哈希:以时间换空间的算法实战
算法
San813_LDD6 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x138702859576 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta19987 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油7 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero8 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称8 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划