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\_pairs[i][0] > sorted\_pairs[j][1] f(i)=max{f(j)+1},ifsorted_pairs[i][0]>sorted_pairs[j][1]

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)
相关推荐
_深海凉_4 分钟前
LeetCode热题100-只出现一次的数字
算法·leetcode·职场和发展
nianniannnn22 分钟前
力扣206.反转链表 92.反转链表II
算法·leetcode·链表
澈20729 分钟前
哈希表实战:从原理到手写实现
算法·哈希算法
旖-旎37 分钟前
哈希表(存在重复元素||)(4)
数据结构·c++·算法·leetcode·哈希算法·散列表
Run_Teenage40 分钟前
Linux:认识信号,理解信号的产生和处理
linux·运维·算法
無限進步D1 小时前
蓝桥杯赛前刷题
c++·算法·蓝桥杯·竞赛
CoderCodingNo1 小时前
【GESP】C++二级真题 luogu-B4497, [GESP202603 二级] 数数
开发语言·c++·算法
磊 子1 小时前
八大排序之冒泡排序+选择排序
数据结构·算法·排序算法
_深海凉_1 小时前
LeetCode热题100-买卖股票的最佳时机
leetcode
We་ct1 小时前
LeetCode 50. Pow(x, n):从暴力法到快速幂的优化之路
开发语言·前端·javascript·算法·leetcode·typescript·