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\_pairs[i][0] > sorted\_pairs[j][1] f(i)=max{f(j)+1},ifsorted_pairs[i][0]>sorted_pairs[j][1]

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)
相关推荐
福尔摩斯张23 分钟前
C语言核心:string函数族处理与递归实战
c语言·开发语言·数据结构·c++·算法·c#
2501_9418846124 分钟前
云计算与边缘计算:解锁未来计算架构的智能边界
leetcode
程序猿小白日记26 分钟前
云计算与物联网融合:推动智慧城市的未来发展
leetcode
吗~喽1 小时前
【LeetCode】滑动窗口_水果成篮_C++
c++·算法·leetcode
yoke菜籽1 小时前
面试150——区间
面试·职场和发展
立志成为大牛的小牛1 小时前
数据结构——四十九、B树的删除与插入
数据结构·学习·程序人生·考研·算法
程序员三藏2 小时前
软件测试之压力测试详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试
高洁012 小时前
具身智能-普通LLM智能体与具身智能:从语言理解到自主行动 (2)
深度学习·算法·aigc·transformer·知识图谱
l1t2 小时前
使用DuckDB SQL求解Advent of Code 2024第9题 磁盘碎片整理
数据库·sql·算法·duckdb·advent of code
小南家的青蛙2 小时前
LeetCode面试题 04.06 后继者
算法·leetcode·职场和发展