Length of the longest sorted subsequence

岁酱吖の 提交于 2019-12-10 20:32:52

问题


My unsorted array is

string[] a = new string[] { "10", "22", "9", "33", "21", "50", "41", "60", "80" };

In this array, 10,22,33,50,60,80 are in ascending order, so the output must be 6.

In general, I want the longest possible length of an ascending list made from elements of the array and starting with the first element.

I have tried this :

string[] a = new string[] { "10", "22", "9", "33", "21", "50", "41", "60", "80" };
List<int> res = new List<int>();
int arrLength = a.Length;
int i = 0;
int prev;
while (i < arrLength)
{
    if (i < arrLength)
    {
        res.Add(Convert.ToInt32(a[i]));
        prev = Convert.ToInt32(a[i]);
        while (Convert.ToInt32(a[++i]) < prev) { }
    }
}

int asdf = res.Count;

but did not succeed.


回答1:


This is called the Longest Ascending Subsequence problem. You can find it using a simple dynamic programming algorithm described in the article.

If all you need is the length of the longest subsequence, you can do it like this:

// length[i] is the length of subsequence ending at position i
var length = new int[a.Length];
for (var i = 0 ; i != length.Length ; i++) {
    // In the worst case a number ends a subsequence of length 1
    length[i] = 1;
    var ai = Convert.ToInt32(a[i]);
    // Go backward on the items that we've seen before
    for (var j = i-1 ; j >= 0 ; j--) {
        var aj = Convert.ToInt32(a[i]);
        // If number at i is greater than the number at j, use the length of j's longest subsequence
        // to calculate the length of the sequence for element at i.
        if (aj > ai && length[j]+1 > length[i]) {
            length[i] = length[j]+1;
        }
    }
}
var res = length.Max();

Your algorithm is incorrect because it uses a "greedy strategy", i.e. it considers any number greater than the previously found one a part of the sorted sequence.




回答2:


You could face problems with this even when you do manager to get it counting correctly.

What if there are more than one ordered set of values to discover, do you want to discover both or just the first one or the one starting with the lowest number etc?

What you need to do first is take the array and create a copy of it in the order you need. You then need to perform checks against the unordered array looping through and comparing the values to the ordered array and breaking out the loop when ordering stops. But you still need to consider the other aspects of what data you want to obtain from doing this and include it in your code.




回答3:


Homework? This is a textbook example of a problem that can be solved using dynamic programming. The array longest will hold the longest increasing subseries ending with the element on the same position.

public int LongestIncreasingSubseries(int[] input)
{
  int[] longest = new int[input.Length];
  int result = 1;
  for(int i = 0; i<input.Length; ++i) 
  {
    longest[i] = 1;
    for(j=0; j<i; ++j) if (input[j]<input[i] && longest[j]+1>longest[i]) longest[i] = longest[j]+1;
    if(longest[j]>result) result = longest[i];
  }
  return result;
}



回答4:


string[] arr = new string[] { "10", "22", "9", "33", "21", "50", "41", "60", "80" };
int biggest = int.MinValue;
int current = 0;

int count = (from str in arr
             where int.TryParse(str, out current)
             let number = current
             where number > biggest
             select biggest = number).Count();

this LINQ query takes each string, and try to convert it into a number. if it succeed, it checks if the number is bigger than the last biggest number. if it does - than storing that number as the new biggest number - and return it.

the Count method just count how much items had returned (but you could store them in an array, or iterating on them in a foreach loop, or anything).




回答5:


This is my answer.

It is easy to undersand you

string[] a = new string[] { "10", "22", "9", "33", "21", "50", "41", "60", "80" };
        List<int> res = new List<int>();
        int arrLength = a.Length;
        int i = 0;
        for (i = 0; i < arrLength; i++)
        {
            if (i < arrLength)
            {
                if (res.Count != 0)
                {
                    if (Convert.ToInt32(a[i]) > res[res.Count - 1])
                    {
                        res.Add(Convert.ToInt32(a[i]));
                    }
                }
                else
                {
                    res.Add(Convert.ToInt32(a[i]));
                }
            }
        }

        int asdf = res.Count;

And the output is 6

See this

and you can download my code here



来源:https://stackoverflow.com/questions/22170397/length-of-the-longest-sorted-subsequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!