首页 > 其他分享 >day43-dynamic programming-part10-8.14

day43-dynamic programming-part10-8.14

时间:2024-08-15 12:57:47浏览次数:7  
标签:nums part10 dynamic len nums2 day43 nums1 dp result

tasks for today:

1. 300.最长递增子序列

2. 674.最长连续递增序列

3. 718.最长重复子数组

--------------------------------------------------------------------------

1. 300.最长递增子序列

In this practice, note the meaning of the dp list: which is: dp[i] signifies the longest ascending sequence that ends with nums[i].

The corresponding initialization of each entry on the dp list is set as 1. 

class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        if len(nums) == 1: return 1
        dp = [1] * len(nums)
        result = 0

        for i in range(len(nums)):
            for j in range(i):
                if nums[i] > nums[j]:
                    dp[i] = max(dp[i], dp[j]+1)
            if dp[i] > result:
                result = dp[i]
        
        return result

2. 674.最长连续递增序列

Compared with last practice 300, the key difference is the requirment on "continuity" of the ascending sequence.

This reduces the trsaverse from two-fold loop to one loop, for each dp[i], there is no need to compare with each dp[j] + 1 with dp[i], 

just need to figure out the relationship between dp[i] and dp[i-1].

class Solution:
    def findLengthOfLCIS(self, nums: List[int]) -> int:
        if len(nums) == 1: return 1
        dp = [1] * len(nums)
        result = 0

        for i in range(1, len(nums)):
            if nums[i] > nums[i-1]:
                dp[i] = dp[i-1] + 1

            if dp[i] > result:
                result = dp[i]
        
        return result

3. 718.最长重复子数组

Note: why there is no "else: dp[i][j] = dp[i-1][j-1]" for each "if", because, the way we define the dp table is "ending with the same nums1[i-1] and nums2[j-1]", so if nums1[i-1] != nums2[j-1], it is not right to do dp[i][j] = dp[i-1][j-1], it should remain 0, which reflected as not updated.

class Solution:
    def findLength(self, nums1: List[int], nums2: List[int]) -> int:
        if (len(nums1) == 1 and nums1[0] in nums2) or (len(nums2) == 1 and nums2[0] in nums1): return 1
        if (len(nums1) == 1 and nums1[0] not in nums2) or (len(nums2) == 1 and nums2[0] not in nums1): return 0

        dp = [[0] * (len(nums2) + 1) for _ in range(len(nums1) + 1)]
        result = 0

        for i in range(1, len(nums1)+1):
            if nums1[i-1] == nums2[0]:
                dp[i][1] = 1
        
        for j in range(1, len(nums2)+1):
            if nums1[0] == nums2[j-1]:
                dp[1][j] = 1

        for i in range(1, len(nums1)+1):
            for j in range(1, len(nums2)+1):
                if nums1[i-1] == nums2[j-1]:
                    dp[i][j] = dp[i-1][j-1] + 1

                if dp[i][j] > result:
                    result = dp[i][j]

        return result

标签:nums,part10,dynamic,len,nums2,day43,nums1,dp,result
From: https://blog.csdn.net/bbrruunnoo/article/details/141187537

相关文章

  • day44-dynamic programming-part11-8.15
    tasksfortoday:1.1143.最长公共子序列2.1035.不相交的线3.53.最大子序和4.392.判断子序列(编辑距离问题)------------------------------------------------------------------------------1.1143.最长公共子序列300:single,ascending,non-continuity674:s......
  • 代码随想录算法训练营第43天:动态规划part10:子序列问题
    300.最长递增子序列力扣题目链接(opensnewwindow)给你一个整数数组nums,找到其中最长严格递增子序列的长度。子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7]是数组[0,3,1,6,2,2,7]的子序列。示例1:输入:nums=[10,9,2......
  • 关于C#的Dynamic调用方法前的一些准备的小Demo
    usingSystem;usingSystem.CodeDom.Compiler;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApp1{publicclassTest{publicstaticTestT......
  • 使用dynamic debug帮助调试
    你一定在kernelsourcecode中看过很多pr_debug()/dev_dbg()/print_hex_dump_debug()吧,这些debug语句提供更多的信息帮助我们了解内核运行流程或是定位问题,可以在运行时按per-callsite单独开启/关闭。那我们来看一下它是如何实现和使用的吧。一、kernel configuration在编译时,......
  • ASTGNN (Learning Dynamics and Heterogeneity of Spatial-Temporal Graph Data forTr
    引言    时空神经网络(STGNNs)被广泛应用于交通预测问题中,在STGNNs中每个节点代表一个交通监测站,边表示道路网络。        在动态预测中,物理量x(t)随时间的变化模型是一个黑盒模型,我们要做的事情就是对黑盒模型进行建模。线性自回归方法直接将动态变化规律看......
  • Dynamics 365 online查询共享给某个Team的记录,然后取消共享
    intiSuccess=0;intiFaile=0;varadminService=CrmServiceClientCommon.GetService();//创建QueryExpression对象QueryExpressionquery=newQueryExpression("opportunity");......
  • 发现这个有趣的问题Dynamic Planning Approach for Radio Tower Placement in Cities
    在ByteLand中,沿轴有n(≤105)个城市,第i个城市位于(A₁,0)(对于1≤i≤n,A≤10°)。在ByteLand,有一家制造无线电塔的电信公司。每个塔的覆盖半径为d,即,当且仅当x-y≤d时,(y,0)处的塔可覆盖(2,0)处的城市。提示:1.目标解决方案基于动态规划,2.问题陈述本身包含有关......
  • Fotify扫描问题Dynamic Code Evaluation:Code Injection
    在使用fotify代码扫描时,程序中JavaScript的eval()函数使用的地方会报DynamicCodeEvaluation:CodeInjection,解释为动态代码评估、代码注入,Web开发中。这两种风险都可能导致严重的安全问题.其安全问题大致描述为1、动态执行的代码可能会包含恶意代码,导致安全漏洞......
  • 将dynamicTemplate添加到谷歌云模板启动
    我们使用谷歌云功能通过以下方式启动模板:https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.locations.templates/launch我们想添加一个通过具有以下布局的动态模板将请求的暂存位置:DYNAMICTEMPLATE={"gcsPath":GCSPATH,"stagingLocation"......
  • Dynamics 365 插件的优缺点
    在现代企业环境中,客户关系管理(CRM)系统如Dynamics365扮演着至关重要的角色。为了满足特定业务需求,企业常常需要对系统进行自定义和扩展。Dynamics365插件(Plugin)正是实现这一目的的重要工具。本文将探讨使用Dynamics365插件的优缺点。插件的优点1.自动化流程插件......