简体   繁体   中英

How to replace the content of largest rectangle in histogram

Can you please tell me, how can I replace the X with O, where the largest rectangle in the histogram is?

Here is my code to generate a histogram:

    public static void histogram() {

    double[] data1 = { 4.1, 6.2, 4.5, 8.0, 7.1, 6.2, 6.3, 0.1, 4.5, 5.1 };

    for (int columns = data1.length; columns >= 1; columns--) {
        for (int zeilen = 0; zeilen < data1.length; zeilen++) {
            if (data1[zeilen] >= columns) {
                System.out.print("X");
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    histogram();
}

The output should be roughly like this:

   X      
   XX     
 X OOOO   
 X OOOO  X
XXXOOOO XX
XXXOOOO XX
XXXOOOO XX
XXXOOOO XX

OR like this:

   X      
   XX     
 X XXXX   
 X XXXX  X
OOOOOOO XX
OOOOOOO XX
OOOOOOO XX
OOOOOOO XX

Just where die largest field is.

The simplest way to do is to find the max value in your array first.

int max=0;
for (int i = 0; i < data1.length; i++) {
    if (data1[i] > max)
        max = data1[i];
}

Then in your loop, check if the data you are working with equals to the maximum value, and use "O" to display.

if (data1[zeilen] == max)
    System.out.println("O");
else
    System.out.println("X");

Then your maximum rectangle will be with "O" .

One way would be to determine which index value in the array holds the largest element. You could do this using a loop or streams:

int maxIndex = IntStream.range(0, data1.length)
    .reduce((i1, i2) -> data1[i1] > data1[i2] ? i1 : i2)
    .orElseGet(() -> 0); // If only one element, max must be first

Then when printing out the histogram you can select the character to output based on whether the indexes match.

for (int columns = data1.length; columns >= 1; columns--) {
  for (int zeilen = 0; zeilen < data1.length; zeilen++) {
    if (data1[zeilen] >= columns) {
      System.out.print(zeilen == maxIndex ? 'O' : 'X');
    } else {
      System.out.print(" ");
    }
  }
  System.out.println();
}

I'm not sure that your range for columns in the first loop (from data1.length down to 1 ) is a great choice. From (int)data1[maxIndex] down to 1 might be better.


OK, the edit makes it a more interesting problem. The simplest way to solve this may be by recursive subdivision:

  1. For range [start, end) , find the histogram index with the minimum value, index . This can be calculated similarly to the max index value code above.

  2. Repeat step 1 for [start, index) recursively if start < index .

  3. Repeat step 1 for [index + 1, end) recursively if index + 1 < end .

  4. Return the rectangle with maximum area from steps 1, 2 and 3.

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