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)
相关推荐
hahaha601627 分钟前
Shades-of-Gray (SoG) 算法--白平衡算法
人工智能·嵌入式硬件·算法·计算机视觉
青山木39 分钟前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
夊1 小时前
C语言第十一课:指针(五)
算法
小师兄吃牛肉1 小时前
C语言指针进阶全解析
java·c语言·算法
梦帮科技1 小时前
vLLM / TensorRT-LLM 极限推理:PagedAttention 细粒度物理页表管理与连续批处理(Continuous Batching)实战
数据结构·人工智能·分布式·python·深度学习·算法·vllm
JAVA面经实录9172 小时前
线上故障标准处理流程【完整版|面试背诵+生产落地】
java·面试·职场和发展
yuanxi2002 小时前
青海共和百万千瓦光伏光热项目并网发电:大客户销售如何用价值力抓住能源大单
大数据·职场和发展·能源·创业创新·学习方法
做运维的阿瑞2 小时前
多表查询基础:INNER JOIN、LEFT JOIN、RIGHT JOIN
数据库·sql·mysql·算法
大侠归来3 小时前
C语言素数判断:从入门到进阶
java·c语言·算法
v:PLLucky203 小时前
农作物病虫害检测识别系统
深度学习·scrapy·算法·百度·微信公众平台