Skip to main content
added 98 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 15
  • 24
public static List<Integer> findLongestIncreasingSequence(int[] numbersToBeProcessed) {
    if (numbersToBeProcessed.length == 0) {
        return null;
    }

    List<Integer> longestIncreasingSequence = new ArrayList<Integer>();
    List<Integer> currentNumbersSequence = new ArrayList<Integer>();

    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i];
        int previousNumber = numbersToBeProcessed[i - 1];
        currentNumbersSequence.add(previousNumber);

        if (currentNumber <= previousNumber) {
            // checks if the current sequence is bigger
            if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
                longestIncreasingSequence.clear();
                longestIncreasingSequence.addAll(currentNumbersSequence);
            }
            // clear the current sequence so it can start all over again
            currentNumbersSequence.clear();
        }
    }
    // at the end of the loop always compare the two sequences.
    if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
        return currentNumbersSequence;
    }
    return longestIncreasingSequence;
}
    if (numbersToBeProcessed.length == 0) {
        return null;
    }

    List<Integer> longestIncreasingSequence = new ArrayList<Integer>();
    List<Integer> currentNumbersSequence = new ArrayList<Integer>();

    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i];
        int previousNumber = numbersToBeProcessed[i - 1];
        currentNumbersSequence.add(previousNumber);

        if (currentNumber <= previousNumber) {
            // checks if the current sequence is bigger
            if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
                longestIncreasingSequence.clear();
                longestIncreasingSequence.addAll(currentNumbersSequence);
            }
            // clear the current sequence so it can start all over again
            currentNumbersSequence.clear();
        }
    }
    // at the end of the loop always compare the two sequences.
    if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
        return currentNumbersSequence;
    }
    return longestIncreasingSequence;
public static List<Integer> findLongestIncreasingSequence(int[] numbersToBeProcessed) {
    if (numbersToBeProcessed.length == 0) {
        return null;
    }

    List<Integer> longestIncreasingSequence = new ArrayList<Integer>();
    List<Integer> currentNumbersSequence = new ArrayList<Integer>();

    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i];
        int previousNumber = numbersToBeProcessed[i - 1];
        currentNumbersSequence.add(previousNumber);

        if (currentNumber <= previousNumber) {
            // checks if the current sequence is bigger
            if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
                longestIncreasingSequence.clear();
                longestIncreasingSequence.addAll(currentNumbersSequence);
            }
            // clear the current sequence so it can start all over again
            currentNumbersSequence.clear();
        }
    }
    // at the end of the loop always compare the two sequences.
    if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
        return currentNumbersSequence;
    }
    return longestIncreasingSequence;
}
deleted 1 character in body
Source Link
Gentian Kasa
  • 2.1k
  • 15
  • 24
    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i + 1];
        int previousNumber = numbersToBeProcessed[i];
        currentNumbersSequence.add(previousNumber);

        // rest of the code

        // you should also remove the 'currentNumbersSequence.add(previousNumbercurrentNumber);' line
    }
    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i + 1];
        int previousNumber = numbersToBeProcessed[i];
        currentNumbersSequence.add(previousNumber);

        // rest of the code

        // you should also remove the 'currentNumbersSequence.add(previousNumber);' line
    }
    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i + 1];
        int previousNumber = numbersToBeProcessed[i];
        currentNumbersSequence.add(previousNumber);

        // rest of the code

        // you should also remove the 'currentNumbersSequence.add(currentNumber);' line
    }
added 96 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 15
  • 24

The former should be extracted and put into a method or just return the longest sequence if you're out of the loop (and this takes care of your concern #2). The latter can just be taken out of the if condition. The code inside the for loop can be written as follows:

This change allows you to remove the currentNumbersSequence.add(numbersToBeProcessed[0]); line.

So, in short, the method should become something like the following (not tested):

    if (numbersToBeProcessed.length == 0) {
        return null;
    }

    List<Integer> longestIncreasingSequence = new ArrayList<Integer>();
    List<Integer> currentNumbersSequence = new ArrayList<Integer>();

    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i];
        int previousNumber = numbersToBeProcessed[i - 1];
        currentNumbersSequence.add(previousNumber);

        if (currentNumber <= previousNumber) {
            // checks if the current sequence is bigger
            if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
                longestIncreasingSequence.clear();
                longestIncreasingSequence.addAll(currentNumbersSequence);
            }
            // clear the current sequence so it can start all over again
            currentNumbersSequence.clear();
        }
    }
    // at the end of the loop always compare the two sequences.
    if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
        return currentNumbersSequence;
    }
    return longestIncreasingSequence;

The former should be extracted and put into a method (and this takes care of your concern #2). The latter can just be taken out of the if condition. The code inside the for loop can be written as follows:

This change allows you to remove the currentNumbersSequence.add(numbersToBeProcessed[0]); line.

The former should be extracted and put into a method or just return the longest sequence if you're out of the loop (and this takes care of your concern #2). The latter can just be taken out of the if condition. The code inside the for loop can be written as follows:

This change allows you to remove the currentNumbersSequence.add(numbersToBeProcessed[0]); line.

So, in short, the method should become something like the following (not tested):

    if (numbersToBeProcessed.length == 0) {
        return null;
    }

    List<Integer> longestIncreasingSequence = new ArrayList<Integer>();
    List<Integer> currentNumbersSequence = new ArrayList<Integer>();

    for (int i = 0; i < numbersToBeProcessed.length - 1; i++) {

        int currentNumber = numbersToBeProcessed[i];
        int previousNumber = numbersToBeProcessed[i - 1];
        currentNumbersSequence.add(previousNumber);

        if (currentNumber <= previousNumber) {
            // checks if the current sequence is bigger
            if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
                longestIncreasingSequence.clear();
                longestIncreasingSequence.addAll(currentNumbersSequence);
            }
            // clear the current sequence so it can start all over again
            currentNumbersSequence.clear();
        }
    }
    // at the end of the loop always compare the two sequences.
    if (currentNumbersSequence.size() > longestIncreasingSequence.size()) {
        return currentNumbersSequence;
    }
    return longestIncreasingSequence;
added 96 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 15
  • 24
Loading
added 414 characters in body
Source Link
Gentian Kasa
  • 2.1k
  • 15
  • 24
Loading
Source Link
Gentian Kasa
  • 2.1k
  • 15
  • 24
Loading