简体   繁体   中英

How to find length of longest sorted subsequence

Given a sequence of integer of length and prints the length of the longest sorted subsequence. If there is more than one subsequence of equal maximum length, the subsequence that appears first (the one with the smallest index) is to be used for the output

For the sequence: 8 2 3 4 5 6 0 10 26 24. The max length is 5. Here is the code i have so far. THank you guys. Note: i am not suppose to use array. I have to use Ctrl+Z to display the max sorted subsequence length.

import java.io.IOException;

public class RepeatingCharacterPositions {
    public static void main(String[] s) throws IOException {
        int counter = 0;
        int inputValue;
        int previousNum = 0;
        int nextNum;
        while ((inputValue = System.in.read()) != -1) {
            nextNum = (int) inputValue;
            if (previousNum < nextNum) {
                counter++;
            } else if (previousNum > nextNum) {
                continue;
            }
            previousNum = nextNum;
        }
        System.out.println("Number of repeating " + counter);
    }
}

A few notes:

  1. Your continue clause prevents you from setting previousNum = nextNum.
  2. You probably need to reset counter when you find a decreasing number. Which means you also probably need to have a second variable that tracks the longest sequence you've found.
  3. You're not handling two consecutive equal values, at least not explicitly. You should consider using a >= or <= in whichever of your tests makes sense.

      if (previousNum < nextNum) { counter++; } else if (previousNum > nextNum) { if(counter > longestSeq) { longestSeq = counter; } counter = 1; } previousNum = nextNum; 

I believe that you're missing a few things.

  • You need to remember the highest value your counter reached
  • In order to get an int from System.in , you should use a Scanner which provides the method Scanner#nextInt
  • If two subsequent numbers are the same, I think that they ARE ordered. Your test should be if (previousNum <= nextNum)
  • If a given number is lower than the previous one, you should "reset" counter to the value of 1 (because any number IS itself an ordered sequence of length 1 )

For example:

public static void main(final String[] s) {
    int counter = 0;
    int highestCount = 0;
    int previousNum = 0;
    int position = 0;
    int latestStartingPosition = 0;
    int startingPosition = 0;

    int inputValue;
    try (final Scanner sc = new Scanner(System.in)) {
        while ((inputValue = sc.nextInt()) != -1) {
            if (previousNum <= inputValue) {
                counter++;
                if (counter > highestCount) {
                    highestCount = counter;
                    startingPosition = latestStartingPosition;
                }
            } else {
                latestStartingPosition = position;
                counter = 1;
            }

            previousNum = inputValue;
            position++;
        }
    }

    System.out.println("starting pos: " + startingPosition);
    System.out.println("Number of repeating positions: " + highestCount);
}

I hope it helps!


EDIT: Answering additional question asked in comments.
To get the index of the beginning of the longest ordered sequence, you have to:

  • have an int to know your current position at any instant
  • have a reference to the last starting position you've encountered - this value is changed every time you reset your primary counter .
  • when you detect that a sequence is longer than any other you've ever had before, then the starting position of THAT sequence should be the final starting position : just save that information any time you save the highest value your counter has reached.

See updated example in the code above :)

Besides what ccjmne pointed out, you can always use a separate method to do this:

public class RepeatingCharacterPositions {
    public static int findLongestSorted(int[] array) {
        int maxCount = 0, count = 0;
        int prev = array[0];
        for(int num : array) {
            if(prev <= num) {
                count++;
            } else {
                count = 1;
            }
            if(count > maxCount)
                maxCount = count;
            prev = num;
        }
        return maxCount;
    }

    public static void main(String[] s) throws IOException {
        int[] arr = {8, 2, 3, 4, 5, 6, 0, 10, 26, 24};
        System.out.println("Number of repeating positions " + findLongestSorted(arr));
    }
}

Note that prev is initialized to be the first element in the array since array with only one element is considered to be sorted. After execution of the program, it will print out

Number of repeating positions 5

If you test on array with only one element, eg,

int[] arr = {8};

It will output:

Number of repeating positions 1

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