简体   繁体   中英

Longest Increasing Subsequence problem (leetcode)

link of the problem:

[link] https://leetcode.com/problems/longest-increasing-subsequence/

class Solution {
public:
int lengthOfLIS(vector& nums)
{
int n=nums.size();
if (n==0)
return 0;

    int list[n];
    for(int i=0;i<n;i++)
        list[i]=0;
    
    list[0]=1;
    for(int i=1;i<n;i++)
    {
        for(int j=i-1;j>=0;j--)
        {
            if(nums[j]<nums[i])
                list[i]=max(list[i],1+list[j]);
        }
    }
    int ans=1;
    for(int i=0;i<n;i++)
        ans=max(ans,list[i]);
    return ans;
}
};

Input: [10,9,2,5,3,7,101,18]

output is coming: 3

expected output: 4

not getting it where its wrong.

#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>

class Solution
{
public:
  int lengthOfLIS(std::vector<int> &);
  int max(int, int);
  std::string print(std::vector<int> const &);
};

int Solution::max(int a, int b)
{
  return a < b ? b : a;
}

std::string Solution::print(std::vector<int> const &input)
{
  std::stringstream ss;
    for (int i = 0; i < input.size(); i++)
        ss << input.at(i) << ' ';

  return ss.str();
}

int Solution::lengthOfLIS(std::vector<int> &nums)
{
  int n = nums.size();
  
  if (n == 0)
    return 0;

  int list[n];
  std::fill_n(list, n, 1);
  //list[0] = 1;

  for (int i = 1; i < n; i++)
  {
    int min_val = nums[i];

    for (int j = i - 1; j > -1; j--)
    {
      if (nums[j] < nums[i] && nums[j] < min_val)
      {
        list[i]++;
        min_val = nums[j];
      }
    }
  }

  int ans = 1;

  for(int i = 0; i < n; i++)
    ans = max(ans, list[i]);

  return ans;
}

int main()
{
  std::vector<int> Input0 { 10, 9, 2, 5, 3, 7, 101, 18 },
    Input1 { 10, 19, 2, 5, 3, 7, 101, 18 },
    Input2 { 10, 9, 12, 5, 3, 7, 101, 18 },
    Input3 { 10, 9, 2, 15, 3, 7, 101, 18 },
    Input4 { 10, 9, 2, 5, 13, 7, 101, 18 },
    Input5 { 10, 9, 2, 5, 3, 17, 101, 18 },
    Input6 { 10, 9, 2, 5, 13, 7, 10, 18 },
    Input7 { 10, 9, 2, 5, 13, 7, 101, 180 };
  Solution solution;
  std::cout << solution.print(Input0) << "\t|\t" << solution.lengthOfLIS(Input0) << std::endl;
  std::cout << solution.print(Input1) << "\t|\t" << solution.lengthOfLIS(Input1) << std::endl;
  std::cout << solution.print(Input2) << "\t|\t" << solution.lengthOfLIS(Input2) << std::endl;
  std::cout << solution.print(Input3) << "\t|\t" << solution.lengthOfLIS(Input3) << std::endl;
  std::cout << solution.print(Input4) << "\t|\t" << solution.lengthOfLIS(Input4) << std::endl;
  std::cout << solution.print(Input5) << "\t|\t" << solution.lengthOfLIS(Input5) << std::endl;
  std::cout << solution.print(Input6) << "\t|\t" << solution.lengthOfLIS(Input6) << std::endl;
  std::cout << solution.print(Input7) << "\t|\t" << solution.lengthOfLIS(Input7) << std::endl;
  return 0;
}

There is just a slight error in your code, every time you are accessing a new index (ie on each iteration of i), the longest increasing subsequence that can be found for an index is that element itself.

So for each iteration intially you should set list[i] = 1

Or, you can also initialize every element as 1 in list array.

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int n=nums.size();
        if (n==0)
            return 0;

        int list[n];
        for(int i=0;i<n;i++)
            list[i]=0;

        list[0]=1;
        for(int i=1;i<n;i++) {
            list[i] = 1;
            for(int j=i-1;j>=0;j--) {
                if(nums[j]<nums[i])
                list[i]=max(list[i],1+list[j]);
            }
        }
        int ans=1;
        
        for(int i=0;i<n;i++)
            ans=max(ans,list[i]);
        return ans;
    }
};

Using std::lower_bound , this'd pass:

#include <cstdint>
#include <vector>
#include <algorithm>

static const struct Solution {
    static const int lengthOfLIS(
        const std::vector<int> &nums
    ) {
        std::vector<int> longest;

        for (std::size_t index = 0; index < nums.size(); index++) {
            const auto iter = std::lower_bound(longest.begin(), longest.end(), nums[index]);

            if (iter == longest.end()) {
                longest.emplace_back(nums[index]);

            } else {
                *iter = nums[index];
            }
        }

        return longest.size();
    }
};

References

  • For additional details, please see the Discussion Board where you can find plenty of well-explained accepted solutions with a variety of languages including low-complexity algorithms and asymptotic runtime / memory analysis 1 , 2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM