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)
相关推荐
geovindu2 小时前
python: PCA
开发语言·后端·python·算法
凯程序猿1号3 小时前
基于射线法与球面外积的WGS84电子围栏点在多边形内判定及边界容差算法
算法·电脑
budingxiaomoli3 小时前
动态规划---子序列
算法·动态规划
释厄6233 小时前
尺子必回量与劣币生良币——可证伪明的尺子性·同源可转化
大数据·算法·开源
天诰4 小时前
SA-T-06 轨迹数据工程 脏数据检测
人工智能·python·算法·语言模型·ai编程
Shaoshing6 小时前
LeetCode 921.使括号括号有效的最少添加
java·算法·leetcode·职场和发展
xurileidian6 小时前
第 30 篇 · 边玩边学Python · 猜数字小游戏 把判断循环异常串起来
前端·python·算法
电科_银尘6 小时前
【大模型算法与工程 65】小数据VLM微调:图文格式、任务与损失
深度学习·算法·机器学习
远航计算机6 小时前
豆包GEO算法实证:引用池到答案之间,还有8.3%入选与13%失配两道关
人工智能·深度学习·算法
我命由我123456 小时前
Excel - Excel 固定列,滚动时该列始终可见
运维·学习·职场和发展·excel·职场发展·运维开发·学习方法