My C++ DP solution - short and easy to understand!


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

Log in to reply
 

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