C++ solution, simple DP


  • 0
    U

    No need for explanation I guess:D
    recursive function:f[k] = max(f[i]+1) where i < k && nums[i] < nums[k]

    class Solution {
    public:
        int lengthOfLIS(vector<int>& nums) {
            int ans = 0;
            int n = nums.size();
            vector<int> f(n, 1);
            if (!n) return 0;
            if (n == 1) return 1;
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < i; j++) {
                    if (nums[j] < nums[i])
                        f[i] = max(f[i], f[j] +1);
                }
                ans = max(ans, f[i]);
            }
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.