首页 > 其他分享 >Leetcode刷题第十四天-动态规划

Leetcode刷题第十四天-动态规划

时间:2024-02-26 16:15:48浏览次数:27  
标签:re int max len 第十四天 range Leetcode dp 刷题

674:最长连续递增序列

链接:674. 最长连续递增序列 - 力扣(LeetCode)

1 class Solution:
2     def findLengthOfLCIS(self, nums: List[int]) -> int:
3         n=len(nums)
4         dp=[1]*n
5         if(n<1):    return 0
6         for i in range(1,n):
7             if(nums[i]>nums[i-1]):
8                 dp[i]=max(dp[i],dp[i-1]+1)
9         return max(dp)
findLengthOfLCIS

718:最长重复子序列

链接:718. 最长重复子数组 - 力扣(LeetCode)

 1 class Solution:
 2     def findLength(self, nums1: List[int], nums2: List[int]) -> int:
 3         #dp[i][j]数组nums1中第i-1号元素之前与数组nums2中第j-1号元素之前的最长重复子数组
 4         #dp[i][j]=dp[i-1][j-1]+1
 5         m,n=len(nums1),len(nums2)
 6         re=0
 7         dp=[[0 for i in range(n+1)] for j in range(m+1)]
 8         for i in range(1,m+1):
 9             for j in range(1,n+1):
10                 if(nums1[i-1]==nums2[j-1]): dp[i][j]=dp[i-1][j-1]+1
11                 re=max(re,dp[i][j])
12         return re
findLength

1143:最长公共子序列

链接:1143. 最长公共子序列 - 力扣(LeetCode)

 1 class Solution:
 2     def longestCommonSubsequence(self, text1: str, text2: str) -> int:
 3         m,n,=len(text1),len(text2)
 4         dp=[[0 for i in range(n+1)] for j in range(m+1)]
 5         re=0
 6         for i in range(1,m+1):
 7             for j in range(1,n+1):
 8                 if(text1[i-1]==text2[j-1]):  dp[i][j]=dp[i-1][j-1]+1
 9                 else:   dp[i][j]=max(dp[i-1][j],dp[i][j-1])
10             re=max(re,max(dp[i]))
11         print(dp)
12         return re
longestCommonSubsequence

1035:不相交的线

链接:1035. 不相交的线 - 力扣(LeetCode)

和1143一样

 1 class Solution:
 2     def maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
 3         #dp[]
 4         m,n=len(nums1),len(nums2)
 5         dp=[[0 for i in range(n+1)] for j in range(m+1)]
 6         re=0
 7         for i in range(1,m+1):
 8             for j in range(1,n+1):
 9                 if(nums1[i-1]==nums2[j-1]): dp[i][j]=dp[i-1][j-1]+1
10                 else:   dp[i][j]=max(dp[i-1][j],dp[i][j-1])
11             re=max(re,max(dp[i]))
12         return re
maxUncrossedLines

53:最大字数和

链接:53. 最大子数组和 - 力扣(LeetCode)

1 class Solution:
2     def maxSubArray(self, nums: List[int]) -> int:
3         n=len(nums)
4         dp=[0]*n
5         dp[0]=nums[0]
6         for i in range(1,n):
7             dp[i]=max(nums[i],dp[i-1]+nums[i])
8         #print(dp)
9         return max(dp)
maxSubArray

392:判断子序列

链接:392. 判断子序列 - 力扣(LeetCode)

 1 class Solution:
 2     def isSubsequence(self, s: str, t: str) -> bool:
 3         n,m=len(s),len(t)
 4         re=0
 5         dp=[[0 for i in range(m+1)] for j in range(n+1)]
 6         for i in range(1,n+1):
 7             for j in range(1,m+1):
 8                 if(s[i-1]==t[j-1]):    dp[i][j]=dp[i-1][j-1]+1
 9                 else:   dp[i][j]=dp[i][j-1]
10             re=max(re,max(dp[i]))
11         print(dp)
12         if(re==n):  return True
13         return False
isSubsequence

115:不同的子序列

链接:115. 不同的子序列 - 力扣(LeetCode)

 1 class Solution:
 2     def numDistinct(self, s: str, t: str) -> int:
 3         #获取指定子序列的方法
 4         m,n=len(s),len(t)
 5         dp=[[0 for i in range(n+1)]for j in range(m+1)]
 6         for i in range(m+1):    dp[i][0]=1
 7         for i in range(1,m+1):
 8             for j in range(1,n+1):
 9                 if(s[i-1]==t[j-1]): dp[i][j]=dp[i-1][j-1]+dp[i-1][j]
10                 else:   dp[i][j]=dp[i-1][j]
11         return dp[-1][-1]
numDistinct

583:两个字符串的删除操作

链接:583. 两个字符串的删除操作 - 力扣(LeetCode)

 1 class Solution:
 2     def minDistance(self, word1: str, word2: str) -> int:
 3         m,n=len(word1),len(word2)
 4         dp=[[0 for i in range(m+1)]for j in range(n+1)]
 5         for i in range(n+1):    dp[i][0]=i
 6         for i in range(m+1):    dp[0][i]=i
 7         for i in range(1,n+1):
 8             for j in range(1,m+1):
 9                 if(word1[j-1]==word2[i-1]): dp[i][j]=dp[i-1][j-1]
10                 else:   dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+2)
11         return dp[-1][-1]
minDistance

72:编辑距离

链接:72. 编辑距离 - 力扣(LeetCode)

修改单词有三种方式:增、删、换

word1删i-1元素dp[i-1][j],word2删j-1元素dp[i][j-1],word1和word2都删dp[i-1][j-1]+2 替换dp[i-1][j-1]+1,替换后word1[i-1]=word2[j-1] 添加=删除的逆向操作,word1增加一个单吃=word2减去一个单词
 1 class Solution:
 2     def minDistance(self, word1: str, word2: str) -> int:
 3         len1,len2=len(word1),len(word2)
 4         dp=[[0 for i in range(len2+1)]for j in range(len1+1)]
 5         for i in range(len1+1): dp[i][0]=i
 6         for i in range(len2+1): dp[0][i]=i
 7         for i in range(1,len1+1):
 8             for j in range(1,len2+1):
 9                 if(word1[i-1]==word2[j-1]): dp[i][j]=dp[i-1][j-1]
10                 else:   dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+1)
11                 #word1删i-1元素dp[i-1][j],word2删j-1元素dp[i][j-1],word1和word2都删dp[i-1][j-1]+2
12                 #替换dp[i-1][j-1]+1
13                 #添加=删除的逆向操作
14         return dp[-1][-1]
minDistance

647:回文子串

链接:647. 回文子串 - 力扣(LeetCode)

 1 class Solution:
 2     def countSubstrings(self, s: str) -> int:
 3         #dp[i][j]  i到j范围内串是否是回文
 4         #s[i]=s[j] j-i<=1,aa,s[i:j]是回文,j-i>1,dp[i+1][j-1]是回文,s[i:j]是回文
 5         n,re=len(s),0
 6         dp=[[False for i in range(len(s))]for j in range(len(s))]
 7         for i in range(len(s)-1,-1,-1):
 8             for j in range(i,len(s)):
 9                 if(s[i]==s[j]):
10                     if(j-i<=1):  
11                         dp[i][j]=True
12                         re+=1
13                     elif(dp[i+1][j-1]):
14                         dp[i][j]=True
15                         re+=1
16         return re
17                 
countSubstrings

516:最长回文子序列

链接:516. 最长回文子序列 - 力扣(LeetCode)

 1 class Solution:
 2     def longestPalindromeSubseq(self, s: str) -> int:
 3         #dp初始化,i=j,dp[i][j]=1
 4         #遍历顺序:i倒序,j正序
 5         n=len(s)
 6         dp=[[0 for i in range(n)]for j in range(n)]
 7         for i in range(n):    dp[i][i]=1
 8         for i in range(n-1,-1,-1):
 9             for j in range(i+1,n):
10                 if(s[i]==s[j]): dp[i][j]=dp[i+1][j-1]+2
11                 else:   dp[i][j]=max(dp[i+1][j],dp[i][j-1])
12         return dp[0][-1]
longestPalindromeSubseq

 

标签:re,int,max,len,第十四天,range,Leetcode,dp,刷题
From: https://www.cnblogs.com/xiaoruru/p/18034572

相关文章

  • LeetCode] 2476. Closest Nodes Queries in a Binary Search Tree
    Youaregiventherootofabinarysearchtreeandanarrayqueriesofsizenconsistingofpositiveintegers.Finda2Darrayanswerofsizenwhereanswer[i]=[mini,maxi]:miniisthelargestvalueinthetreethatissmallerthanorequaltoqueries[......
  • Leetcode刷题第十三天-动态规划
    198:打家劫舍链接:198.打家劫舍-力扣(LeetCode)线性数组1classSolution:2defrob(self,nums:List[int])->int:3#dp[i]偷房间i能获得的最大价值4#推导公式dp[i]=max(dp[i-2]+nums[i],dp[i-1]):dp[i-1]不偷房间i,dp[i-2]+nums[i]偷房间i5......
  • 【leetcode】数组篇刷题 --滑动窗口
    /**@lcapp=leetcode.cnid=209lang=cpp**[209]长度最小的子数组*找最短的子数组*///@lccode=startclassSolution{public:intminSubArrayLen(inttarget,vector<int>&nums){//滑动窗口,//一个计算总和intsum=0;......
  • Leetcode 560 和为k的子数组
    Problem:560.和为K的子数组难点怎么通过前缀和找到和为k的子数组如官方题解所言,[j···i]的子数组=k可转化为pre[i]-pre[j-1]==k要找到前缀和找到和为k的子数组个数就是“找到当前前缀和pre[i]-之前求得的前缀和=k”的总情况。我们通过哈希表记录每个前缀和(的值)出......
  • 【力扣刷题】合并两个有序链表
    题目描述分析这道题实际的解法是要通过递归来写,由于链表的特性:链表的任何一个子表都是链表。所以很多链表的算法用递归来写会非常简便。这里先尝试着写一下非递归的算法,再写一遍递归的算法。非递归:classSolution{public://voidInsert(ListNode*node1,ListNode*n......
  • Leetcode 42.接雨水
    题目朴素解法:对于每列分别向左右扫描查找左右最高的柱子,对于每一个柱子接的水,那么它能接的水=min(左右两边最高柱子)-当前柱子高度。遍历每列时间复杂度为O(n),每列再扫描O(n),总共O(N^2)。classSolution{public:inttrap(vector<int>&height){//O(n^2)还是超......
  • 代码随想录算法训练营day03 | leetcode 203. 移除链表元素、707. 设计链表、206. 反转
    目录题目链接:203.移除链表元素-简单题目链接:707.设计链表-中等题目链接:206.反转链表-简单题目链接:203.移除链表元素-简单题目描述:给你一个链表的头节点head和一个整数val,请你删除链表中所有满足Node.val==val的节点,并返回新的头节点。示例1:输入:head=[1,2,6......
  • 【leetcode】数组篇刷题 --删除元素
    //@before-stub-for-debug-begin#include<vector>#include<string>#include"commoncppproblem27.h"usingnamespacestd;//@before-stub-for-debug-end/**@lcapp=leetcode.cnid=27lang=cpp**[27]移除元素*///@lccode=start......
  • 代码随想录算法训练营day02 | leetcode 977. 有序数组的平方、35.搜索插入位置、34.在
    题目链接:977.有序数组的平方-简单题目描述:给你一个按非递减顺序排序的整数数组nums,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。示例1:输入:nums=[-4,-1,0,3,10]输出:[0,1,9,16,100]解释:平方后,数组变为[16,1,0,9,100]排序后,数组变为[0,1,9,16,100]......
  • Leetcode刷题第十二天-动态规划
    1049:最后一块石头的重量II链接:1049.最后一块石头的重量II-力扣(LeetCode)1classSolution:2deflastStoneWeightII(self,stones:List[int])->int:3#dp[i]背包为i的最大价值为dp[i]4#推导公式dp[i]=max(dp[i],dp[i-stones[i]]+stones[i]......