#coding exercise
print the area of the largest rectangle in a histogram of unit width heights.
public static int MaxArea(ref List<int> h)
{
if (h == null || h.Length <= 0) return 0;
var areas = new List<int>();
for (int i = 0; i < h.Length; i++)
{
areas.add(i, h[i]);
for (int k = i+1; k < h.Length; k++)
{
if (h[k] >= h[i])
areas[i] += h[i];
else
{
break;
}
}
}
return areas.Max();
}
public class hc
{
// height
Public int h { get; set;}
// frequency
Public int c { get; set;}
}
Public static int MaxAreaByStack (List <int> h)
{
if (h == null || h.Length <= 0) return 0;
Int MaxArea =0;
var hts = new stack<int>();
Var f = new List<int>();
Hts.push (h [0]);
F.Add (1);
for (int i = 1; i < h.Length; i++)
{
If (h [i] > h [i-1])
{
For (int k=0; k< hts.count; k++)
{
F [k] +=1;
}
Hts.Push (h [i]);
F.Add (1);
}
Else
{
For ( int k = hts.count -1; k >=0;k--)
{
If (h [k] <= h [i]) break;
Var last = hts.pop ();
Var count = f.Last ();
F.RemoveLast ();
If (last × count > maxArea)
MaxArea = last×count;
}
}
}
Return maxArea;
}
print the area of the largest rectangle in a histogram of unit width heights.
public static int MaxArea(ref List<int> h)
{
if (h == null || h.Length <= 0) return 0;
var areas = new List<int>();
for (int i = 0; i < h.Length; i++)
{
areas.add(i, h[i]);
for (int k = i+1; k < h.Length; k++)
{
if (h[k] >= h[i])
areas[i] += h[i];
else
{
break;
}
}
}
return areas.Max();
}
public class hc
{
// height
Public int h { get; set;}
// frequency
Public int c { get; set;}
}
Public static int MaxAreaByStack (List <int> h)
{
if (h == null || h.Length <= 0) return 0;
Int MaxArea =0;
var hts = new stack<int>();
Var f = new List<int>();
Hts.push (h [0]);
F.Add (1);
for (int i = 1; i < h.Length; i++)
{
If (h [i] > h [i-1])
{
For (int k=0; k< hts.count; k++)
{
F [k] +=1;
}
Hts.Push (h [i]);
F.Add (1);
}
Else
{
For ( int k = hts.count -1; k >=0;k--)
{
If (h [k] <= h [i]) break;
Var last = hts.pop ();
Var count = f.Last ();
F.RemoveLast ();
If (last × count > maxArea)
MaxArea = last×count;
}
}
}
Return maxArea;
}
No comments:
Post a Comment