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)
相关推荐
longlongzihan14 小时前
LeetCode 17电话号码的字母组合:回溯算法(DFS)详解
c++·算法·leetcode·深度优先
封印师请假去地球钓鱼14 小时前
边解边变的问题:从“决策依赖“一词出发
人工智能·算法
今晚打老虎15 小时前
c++之提高A(前缀和)(第三课)
数据结构·c++·算法
财情双生教练易和中16 小时前
“找矿大会战”产业链,量化统计研究报告
职场和发展·职业发展·创业创新·程序员创富
不会就选b16 小时前
算法日常・每日刷题--<贪心>26
数据结构·算法
yuanxi20016 小时前
001267拟扩产光模块:大客户销售如何用价值力读一条扩产公告
人工智能·职场和发展·创业创新·学习方法
Interview Aid11217 小时前
Google SWE Intern Interview Process:OA、Coding 与面试流程详解
面试·职场和发展
hetao173383718 小时前
2026-09-29 hetao1733837 的刷题记录
c++·算法
欣欣之王来了19 小时前
AI合规专项:AI算法透明度的合规要求
人工智能·算法
wzdark19 小时前
分块思想在算法优化中的实践与应用4
算法