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)
相关推荐
天辛大师几秒前
天心大师:不确定中锚定自我,AI生活的哲学命题
人工智能·算法·决策树·机器学习·生活·启发式算法
皓月斯语27 分钟前
B2132 素数对
c++·算法·题解
星轨初途1 小时前
LeetCode 热题 100——day2 字母异位词分组
c++·算法·leetcode
happyprince1 小时前
篇1:整体观 · bitsandbytes项目全貌解读
人工智能·算法
青梅橘子皮2 小时前
STL---map/set... “家族“详解(从使用到底层)(1)
数据结构·算法
让学习成为一种生活方式2 小时前
DNA 甲基化综述(模式、调控与功能)--Current Opinion in Plant Biology
算法
-森屿安年-2 小时前
647. 回文子串
c++·算法·动态规划
皓月斯语3 小时前
P5736 【深基7.例2】质数筛
c++·算法·题解
极序时代GEO品牌优化4 小时前
杭州极序时代|面向地域、设备与用户画像的动态GEO
人工智能·python·算法·极序时代geo
啦啦啦啦啦zzzz4 小时前
贪心算法和动态规划
c++·算法·贪心算法·动态规划