DP solution JAVA
Intuition\n Describe your first thoughts on how to solve this problem. \nEither we include/ exclude the number in longest sub sebquence and maximize the length.
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEither we include/ exclude the number in longest sub sebquence and maximize the length.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Dynamic programming to calculate the longest subsequence.\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: O(n^2) but we can bring down to O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int lengthOfLongestSubsequence(List<Integer> nums, int target) {\n        int[][] dp = new int[nums.size()+1][target+1];\n        for (int r = 1; r <= nums.size(); r++) {\n            int num = nums.get(r-1);\n            for (int c=0; c<=target; c++) {\n                int out = dp[r-1][c];\n                int in = c-num < 0 ? 0 : dp [r-1][c-num];\n                in = in == 0 ? c == num ? 1 : 0 : in+1;\n                dp[r][c] = Math.max(in, out);\n            }\n        }\n        return dp[nums.size()][target] == 0 ? -1 : dp[nums.size()][target];\n\n    }\n}\n```
['Java']