[Java/C++/Python] DP
Explanation\ndp1 means the maximum step ending with A[i].\ndp2 means the maximum step ending with B[i].\n\nThen cosider these 4 transitions:\nt11 for A[i - 1] -
# **Explanation**\n`dp1` means the maximum step ending with `A[i]`.\n`dp2` means the maximum step ending with `B[i]`.\n\nThen cosider these 4 transitions:\n`t11` for `A[i - 1] -> A[i]`\n`t12` for `A[i - 1] -> B[i]`\n`t21` for `B[i - 1] -> A[i]`\n`t22` for `B[i - 1] -> B[i]`\n\n\nWe update\n`dp1 = max(t11, t21)`\n`dp2 = max(t12, t22)`\n`res = max(res, dp1, dp2)`\n<br>\n\n# **Complexity**\nTime `O(n)`\nSpace `O(1)`\n<br>\n\n\n**Java**\n```java\n    public int maxNonDecreasingLength(int[] A, int[] B) {\n        int res = 1, dp1 = 1, dp2 = 1, n = A.length, t11, t12, t21, t22;\n        for (int i = 1; i < n; ++i) {\n            t11 =  A[i - 1] <= A[i] ? dp1 + 1 : 1;\n            t12 = A[i - 1] <= B[i] ? dp1 + 1 : 1;\n            t21 = B[i - 1] <= A[i] ? dp2 + 1 : 1;\n            t22 = B[i - 1] <= B[i] ? dp2 + 1 : 1;\n            dp1 = Math.max(t11, t21);\n            dp2 = Math.max(t12, t22);\n            res = Math.max(res, Math.max(dp1, dp2));\n        }\n        return res;\n    }\n```\n\n**C++**\n```cpp\n    int maxNonDecreasingLength(vector<int>& A, vector<int>& B) {\n        int res = 1, dp1 = 1, dp2 = 1, n = A.size(), t11, t12, t21, t22;\n        for (int i = 1; i < n; ++i) {\n            t11 =  A[i - 1] <= A[i] ? dp1 + 1 : 1;\n            t12 = A[i - 1] <= B[i] ? dp1 + 1 : 1;\n            t21 = B[i - 1] <= A[i] ? dp2 + 1 : 1;\n            t22 = B[i - 1] <= B[i] ? dp2 + 1 : 1;\n            dp1 = max(t11, t21);\n            dp2 = max(t12, t22);\n            res = max(res, max(dp1, dp2));\n        }\n        return res;\n    }\n```\n\n**Python**\n```py\n    def maxNonDecreasingLength(self, A: List[int], B: List[int]) -> int:\n        res = dp1 = dp2 = 1\n        for i in range(1, len(A)):\n            t11 = dp1 + 1 if A[i - 1] <= A[i] else 1\n            t12 = dp1 + 1 if A[i - 1] <= B[i] else 1\n            t21 = dp2 + 1 if B[i - 1] <= A[i] else 1\n            t22 = dp2 + 1 if B[i - 1] <= B[i] else 1\n            dp1 = max(t11, t21)\n            dp2 = max(t12, t22)\n            res = max(res, dp1, dp2)\n        return res\n```\n
['C', 'Python', 'Java']