簡體   English   中英

最長遞增子序列問題(leetcode)

[英]Longest Increasing Subsequence problem (leetcode)

問題鏈接:

[鏈接] 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;
}
};

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

輸出即將到來: 3

預期輸出: 4

沒有得到它的錯誤。

#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;
}

您的代碼中只有一個小錯誤,每次訪問新索引時(即在 i 的每次迭代中),可以為索引找到的最長遞增子序列是該元素本身。

因此,對於最初的每次迭代,您應該設置list[i] = 1

或者,您也可以將列表數組中的每個元素初始化為 1。

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;
    }
};

使用std::lower_bound ,這會通過:

#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();
    }
};

參考

  • 有關其他詳細信息,請參閱討論板,在那里您可以找到大量解釋清楚的公認解決方案,其中包含各種語言,包括低復雜度算法和漸近運行時/內存分析12

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM