Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fATALeRRoR/9eaabf05ed43ac726a87 to your computer and use it in GitHub Desktop.
Save fATALeRRoR/9eaabf05ed43ac726a87 to your computer and use it in GitHub Desktop.
Today at James Michael Hare blog new coding challenge was announced. Here is my solution.
using System;
namespace BarChartApplication
{
public class BarChartAnalyzer
{
int[] _Bars;
public BarChartAnalyzer(int[] bars)
{
_Bars = bars;
}
public void Calculate()
{
Console.WriteLine(string.Join(" ", _Bars));
Console.WriteLine();
int top1 = 0;
int top2 = 0;
int areaSize = 0;
int areaBars = 0;
for (int i = 0; i < _Bars.Length; i++)
{
if ((i == 0 && _Bars[i] > _Bars[i + 1]) || //Top is first element?
(i == _Bars.Length - 1 && _Bars[i] >= _Bars[i - 1]) || //Top is last element?
(i > 0 && _Bars[i] >= _Bars[i-1] && _Bars[i] > _Bars[i + 1]) //Top is element in middle
)
{
//Top found
Console.Write("Top found: {0}. ", _Bars[i]);
if (top1 == 0)
{
//First Top is found
top1 = _Bars[i];
Console.WriteLine("Top1 from: {0}", top1);
}
else
{
//Second Top is found
top2 = _Bars[i];
Console.WriteLine("Top2 to: {0}", top2);
}
}
if (top1 > 0 && top2 == 0)
{
//Calculating area when Top1 found and while Top2 not yet found
areaSize += _Bars[i];
areaBars++;
}
else if (top2 > 0)
{
//Stop calculating area when Top2 found
Console.WriteLine("Area: {0}", areaBars*Math.Min(top1, top2)-areaSize);
Console.WriteLine();
top1 = top2; //Start calculating from last top
Console.WriteLine("Top found: {0}. Top1 from: {0}", top1);
top2 = 0;
areaSize = 0;
areaBars = 0;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment