Java || DP || Memoization
Time Complexity : O(3n)\n# Space Complexity : O(3n) (DP) + O(n) (Stack)\n# Code\n\nclass Solution {\n    private Integer[][] dp;\n    \n    public int maxNonDec
# Time Complexity : $$O(3*n)$$\n# Space Complexity : $$O(3*n) (DP) + O(n) (Stack)$$\n# Code\n```\nclass Solution {\n    private Integer[][] dp;\n    \n    public int maxNonDecreasingLength(int[] nums1, int[] nums2) {\n        int n = nums1.length;\n        dp = new Integer[n][3];\n        return solve(0, 0, n, nums1, nums2);\n    }\n    \n    // choice := what we chose previously = 0 for nothing, 1 for num1, 2 for num2\n    private int solve(int idx, int choice, int n, int[] nums1, int[] nums2) {\n        if(idx == n) return 0;\n        if(dp[idx][choice] != null) return dp[idx][choice];\n        int maxLen = 0;\n        \n        // if previously we haven\'t chosen a value then we can\n        // either skip this value too or take this value\n        if(choice == 0) maxLen = Math.max(maxLen, solve(idx + 1, 0, n, nums1, nums2));\n        int prev = choice == 0 ? -1 : choice == 1 ? nums1[idx - 1] : nums2[idx - 1];\n        if(nums1[idx] >= prev) {\n            maxLen = Math.max(maxLen, 1 + solve(idx + 1, 1, n, nums1, nums2));\n        }\n        if(nums2[idx] >= prev) {\n            maxLen = Math.max(maxLen, 1 + solve(idx + 1, 2, n, nums1, nums2));\n        }\n        return dp[idx][choice] = maxLen;\n    }\n}\n```
['Dynamic Programming', 'Memoization', 'Java']