-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution.cs
50 lines (47 loc) · 1.26 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public int FindMax(int[] space, int x)
{
var chunkNum = 1;
var s = new Stack<int>();
s.Push(0);
for (int i = 1; i < space.Length; i++)
{
// first chunk
if (i < x)
{
if (space[i] < space[s.Peek()])
{
s.Pop();
s.Push(i);
}
}
// other chunks
else
{
// if found minimum is member of current chunk we just need to compare current number with it
var peek = s.Peek();
if (peek >= chunkNum)
{
s.Push(space[i] < space[peek] ? i: peek);
}
// we have to loop through current chunk to find minimum number
else
{
s.Push(i);
var j = chunkNum;
var count = 0;
while (count++ < x)
{
if (space[j] < space[s.Peek()])
{
s.Pop();
s.Push(j);
}
j++;
}
}
// we are ready to go to next chunk
chunkNum++;
}
}
return s.Select(c => space[c]).Max();
}