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)
相关推荐
zephyr0513 分钟前
从递归到迭代:二叉树非递归前中后序遍历详解
算法
evans在进步16 分钟前
LeetCode 2 两数相加:链表模拟加法,Java 图解进位过程
java·leetcode·链表
Dr.kangder17 分钟前
嵌入式总线设备解析——TTE总线应用与实践
开发语言·网络·算法·嵌入式·多任务·同步机制
Hi李耶23 分钟前
【LeetCode】557.反转字符串中的单词 III
算法·leetcode·职场和发展
深蓝学院29 分钟前
机器人学习算法五大体系详解:模仿、强化、多模态、持续学习……
算法·机器人
2401_858286111 小时前
OS82.【Linux】设计线程池
java·linux·运维·服务器·开发语言·算法·线程池
Epiphany.5561 小时前
找到所有好字符串(记忆化搜索+KMP)(里面含KMP模板,用来入门)
算法
坚持编程的菜鸟2 小时前
模拟实现strncpy
c语言·算法·模拟实现strncpy
深圳市快瞳科技有限公司3 小时前
个体识别、行为解读、健康管理:多模态宠物AI大模型的场景化落地
人工智能·算法·计算机视觉·大模型·多模态·宠物·宠物ai识别
wangwangmoon_light3 小时前
1.1 灵神题单总结_滑动窗口与双指针
leetcode