简体   繁体   中英

Working on a Non-decreasing Color Brightness Sequence

I'm working on a project at school where one of the things doing is trying to set up a "Non-decreasing Brightness Sequence" method. To explain: I'm being asked to write code that finds and returns the longest sequence of non-decreasing values. For an example, given the following sequence: 67.8, 91.3, 56.3, 87.3, 87.3, 90.4 , 55.0, the sequence that would be returned is the sequence of values shown in boldface. Essentially, I'm being asked to return an array of Line object references—not values—representing this sequence and I'm having some problems with the program logic and I need help. This is what I've written so far. the method is being written in the LineSet class of my project:

public nonDecrBrightSeq() {
        int maxRow = 1, currentRow = 1 , startIndex = 0;

        for (int i = 0; i < data.length; i++) {
            double b1 = data[i].brightness();
            double b2 = data[i + 1].brightness();
            if (b1 > b2 && currentRow >= maxRow)
                startIndex = i - currentRow;
            if (b2 < b2 && currentRow <= maxRow)
                startIndex = i + currentRow;
            if (b2 = b2 && currentRow == maxRow)
                startIndex = i + currentRow;
        }
        return currentRow;
    }

Also, this is the "brigthness" method that's being substantiated from the "Line" class. The Brightness is calculated based on the red, green, and blue components of a color. The equation I've set up for calculating it is as follows; I was trying to set it up so that that the multiplier isn't squared, just the R, G, and B values:

  public double brightness (Color color) {
        double brightness = Math.sqrt(Math.pow(color.getRed(),2) * .241 + 
                                      Math.pow(color.getGreen(),2) * .691 + 
                                      Math.pow(color.getBlue(),2) * .068);
        return brightness;
    }

My instructor gave me a sign hint. He may want me to make this a sorting method, but is it a bubble, selection or insertion or none of the above:

Assume you're posing a challenge to another person. Tell them you're going to show them a series of playing cards; you'll tell them how many, before showing the first one. They'll be shown each card, one at a time, and will only see the current card (never the entire series of them). You hand them a pad and pencil and tell them they can make any notes they'd like. In the end, they need to tell you the longest non-decreasing sequence of cards, telling you how many cards were in that sequence and what card started the sequence.

If anyone has any insight how I can make the logic of nonDecrBrightSeq better, I would be appreciated. If you need me to show more up my code, let me know.

I'm not sure if I understood your question correctly, but I would use this approach:

var test = [67.8, 56.3, 87.3, 87.3, 90.4, 55.0, 33, 35, 36, 37, 37, 38, 1, 2, 99];

function findLongestNonDecrSeq(data) {
  var maxLength = 0;
  var currLength = 1;
  var startIndex = 0;
  for (var i = 1; i < data.length; i++) {
    if (data[i] < data[i-1]) {
        if (currLength > maxLength) {
        maxLength = currLength;
        startIndex = i - maxLength;
      }
      currLength = 1;
    } else {
      currLength++;
    }
  }
  return {'startIndex':startIndex, 'maxLength': maxLength};
}

var result = findLongestNonDecrSeq(test);
document.write('startIndex: ' + result.startIndex);
document.write('<br>');
document.write('maxLength: ' + result.maxLength);

https://jsfiddle.net/3Lhrjutu/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