leetocde2771. 构造最长非递减子数组 记忆化dfs

给定两个数组nums1和nums2,构造nums3,使得nums3的每个元素可取nums1或nums2相应位置的值,目标是找到nums3中最长的非递减子数组。通过深度优先搜索(DFS)配合记忆化搜索(使用二维数组cache存储中间结果)来解决此问题,以避免重复计算。
摘要由CSDN通过智能技术生成
  • https://leetcode.cn/problems/longest-non-decreasing-subarray-from-two-arrays/

  • 给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,长度均为 n 。

  • 让我们定义另一个下标从 0 开始、长度为 n 的整数数组,nums3 。对于范围 [0, n - 1] 的每个下标 i ,你可以将 nums1[i] 或 nums2[i] 的值赋给 nums3[i] 。

  • 你的任务是使用最优策略为 nums3 赋值,以最大化 nums3 中 最长非递减子数组 的长度。

  • 以整数形式表示并返回 nums3 中 最长非递减 子数组的长度。

  • 注意:子数组 是数组中的一个连续非空元素序列。

示例 1:

输入:nums1 = [2,3,1], nums2 = [1,2,1]
输出:2
解释:构造 nums3 的方法之一是: 
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]
从下标 0 开始到下标 1 结束,形成了一个长度为 2 的非递减子数组 [2,2] 。 
可以证明 2 是可达到的最大长度。
示例 2:

输入:nums1 = [1,3,2,1], nums2 = [2,2,3,4]
输出:4
解释:构造 nums3 的方法之一是: 
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]
整个数组形成了一个长度为 4 的非递减子数组,并且是可达到的最大长度。
示例 3:

输入:nums1 = [1,1], nums2 = [2,2]
输出:2
解释:构造 nums3 的方法之一是: 
nums3 = [nums1[0], nums1[1]] => [1,1] 
整个数组形成了一个长度为 2 的非递减子数组,并且是可达到的最大长度。
 

提示:

1 <= nums1.length == nums2.length == n <= 105
1 <= nums1[i], nums2[i] <= 109

题解

        如果是一维数组,可以直接暴力,二维的话可以dfs(i(起始位置),j(当前位置),value上次选的是哪个值1还是2),这样我们就可以求dfs(0,j,1)和dfs(0,j,2)的最大值了。注:起始位置是倒序的位置

正解

class Solution {
public:
    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {
        int n =nums1.size();
        int res=0;
        vector<vector<int>> cache(n,vector<int>(2,-1));//cache 用数组做cache,需要包括所有状况
        // 枚举所有可能值
        for(int i=0;i<n;i++){
            int res1=dfs(nums1,nums2,i,0,cache);
            int res2=dfs(nums1,nums2,i,1,cache);
            res=max({res,res1,res2});
        }
        return res;
    }
    // dfs(i) 表示以 nums[i] 结尾的最长非递减数组的长度,传入参数有两个,位置以及选择的数来自谁
    int dfs(vector<int>& nums1, vector<int>& nums2,int i,int chose_index,vector<vector<int>>& cache){
        if(i==0) return 1;

        // 运算前先查表
        if( cache[i][chose_index]!= -1 )
            return cache[i][chose_index];


        int res=1;  //i位置选择chose_index时,最短为1
        int curval = chose_index==0 ? nums1[i] : nums2[i];
        //进入两个分支
        if(nums1[i-1]<= curval)
            res=dfs(nums1,nums2,i-1,0,cache)+1;
        if(nums2[i-1]<= curval)
            res=max(res,dfs(nums1,nums2,i-1,1,cache)+1);

         cache[i][chose_index]=res;
        return res;
    }
};

错解

        dfs会超时,然后用了dfs+map进行记忆化,嗯,还是超时了,然后准备用下unordered_map因为其查找效率高,但是unordered_map使用 std::hash 模板来计算其条目的哈希,vector不支持默认哈希(没有默认哈希计算的类型必须定义自定义哈希,下边的图是可以默认哈希计算的)。
在这里插入图片描述

class Solution {
public:
    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {
        int m=nums1.size();
        int res=0;
        map<vector<int>,int> cache;//cache
        // 枚举所有位置的可能
        for(int i=0;i<m;i++){
            // 每个位置又包含两种可能
            int res1=dfs(nums1,nums2,i,0,cache);
            int res2=dfs(nums1,nums2,i,1,cache);
            // 最终返回值是这些中的最大值
            res=max({res,res1,res2});
        }
        return res;
    }
    // dfs(i) 表示以 nums[i] 结尾的最长非递减数组的长度,传入参数有两个,位置以及选择的数来自谁
    int dfs(vector<int>& nums1, vector<int>& nums2,int i,int chose_index,map<vector<int>,int> cache){
        if(i==0) return 1;

        // 运算前先查表
        if( cache.find({chose_index,i})!= cache.end() )
            return cache[{chose_index,i}];

        // 枚举递归入口
        int res=1;  
        if(nums1[i-1]<= (chose_index==0 ? nums1[i] : nums2[i]))
            res=dfs(nums1,nums2,i-1,0,cache)+1;
        if(nums2[i-1]<= (chose_index==1 ? nums2[i] : nums1[i]))
            res=max(res,dfs(nums1,nums2,i-1,1,cache)+1);
        cache[{chose_index,i}]=res;
        return res;
    }
};

相关:

        在leetcode300. 最长递增子序列 子序列(不连续)可以对一个以为dp数组进行两个for循环迭代。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值