Connection Status:
Competition Arena > ValueHistogram
SRM 565 · 2012-06-05 · by gojira_tc · Simple Search, Iteration, String Manipulation
Class Name: ValueHistogram
Return Type: String[]
Method Name: build
Arg Types: (vector<int>)
Problem Statement

Problem Statement

A histogram of a given collection of numbers graphically represents the frequency of each value in the collection. We are given several numbers ranging from 0 to 9 as a int[] values. The goal is to build their histogram according to the following rules.

1) The width of the histogram should be exactly 10.

2) The height of the histogram should equal to H+1, where H is the number of times the most frequent element occurs in values.

3) The i-th (0-based) column of the histogram corresponds to the value i. Let X(i) be the frequency of value i in values. Then the last X(i) characters in the column should be 'X's and the other ones should be '.'s. For example, if value i was not present in values, the column should be filled with '.' characters. If i was present once, the last element of the column should be 'X' and and the other ones should be '.'s. If i was present twice, the last two elements should be 'X's and and the other ones should be '.'s, and so on.

Build the histogram and return it as a String[].

Constraints

  • values will contain between 1 and 50 elements, inclusive.
  • Each element of values will be between 0 and 9, inclusive.
Examples
0)
{2, 3, 5, 5, 5, 2, 0, 8}
Returns: {"..........", ".....X....", "..X..X....", "X.XX.X..X." }

The most frequent value is 5, which occurs 3 times. Hence the height of the histogram is 4. It looks as follows: .......... .....X.... ..X..X.... X.XX.X..X.

1)
{9, 9, 9, 9}
Returns: {"..........", ".........X", ".........X", ".........X", ".........X" }

.......... .........X .........X .........X .........X

2)
{6, 4, 0, 0, 3, 9, 8, 8}
Returns: {"..........", "X.......X.", "X..XX.X.XX" }

.......... X.......X. X..XX.X.XX

3)
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Returns: {"..........", "XXXXXXXXXX" }

.......... XXXXXXXXXX

4)
{6, 2, 3, 3, 3, 7, 8, 1, 0, 9, 2, 2, 4, 3}
Returns: {"..........", "...X......", "..XX......", "..XX......", "XXXXX.XXXX" }

.......... ...X...... ..XX...... ..XX...... XXXXX.XXXX

Submissions are judged against all 86 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ValueHistogram with a public method vector<string> build(vector<int> values) · 86 test cases · 2 s / 256 MB per case

Submitting as anonymous