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)
相关推荐
m沐沐6 小时前
【深度学习】YOLOv2目标检测算法——改进点、网络结构与聚类先验框解析
人工智能·pytorch·深度学习·算法·yolo·目标检测·transformer
不会就选b6 小时前
算法日常・每日刷题--<链表>3
数据结构·算法·链表
测试界的世清6 小时前
各大厂软件测试面试题+答案纯干货
面试·职场和发展
geovindu6 小时前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
SunShaoLu7 小时前
实测7款思维导图工具图片导出能力:PNG/SVG/PDF全支持,附选型指南
职场和发展·效率工具·思维导图
Zachery Pole8 小时前
CCF-CSP备战NO.7【队列】
算法
闪电悠米8 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展
满天星83035779 小时前
【算法】最长递增子序列(三种解法)
算法
啦啦啦啦啦zzzz9 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法
清泓y11 小时前
RAG 技术
算法·ai