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)
相关推荐
行者全栈架构师2 分钟前
【共创稿事节】双引擎智能相册:HarmonyOS 7(API 26)文搜图 × 图像超分端侧 AI 联动实战
前端·算法·架构
圣保罗的大教堂15 分钟前
leetcode 3644. 排序排列 中等
leetcode
背景调查知识库18 分钟前
第三方背景调查中的企业委托方与受托方角色核验什么?
职场和发展
小蒋观天下21 分钟前
【无标题】红外补光近距过曝、远距欠曝:导致车牌AI识别失效调试
人工智能·深度学习·数码相机·算法·机器学习·ai大模型
人才风控数字化33 分钟前
在职风控系统中的误报率、有效率与模型评估如何判断?
职场和发展
吞下星星的少年·-·2 小时前
牛客周赛 119B题(贪心)
算法·贪心
NEFU_Jyj2 小时前
牛客周赛R166(max型瓶颈最短路,前后缀DP合并)
算法·深度优先
Tisfy2 小时前
LeetCode 3644.排序排列:不对则与(脑筋急转弯)
数据结构·算法·leetcode·题解·位运算
文丑颜不良啊3 小时前
LeetCode576~600题目解析
java·leetcode
lisw053 小时前
分岔论揭示演化突变密码
开发语言·算法·数学建模