-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextLargerElement.cs
More file actions
90 lines (76 loc) · 1.69 KB
/
NextLargerElement.cs
File metadata and controls
90 lines (76 loc) · 1.69 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] input = {11, 4, 3, 3, 6, 3, 7, 2, 9};
int[] expected = {-1, 3, 2, 1, 2, 1, 2, 1, -1};
Console.WriteLine("Input");
WriteLine(input);
Console.WriteLine("Expected");
WriteLine(expected);
var stackResult = NextLarger_Stack(input);
var loopResult = NextLarger_Loop(input);
VerboseVerify(expected, stackResult, "Stack");
VerboseVerify(expected, loopResult, "Loop");
}
//simple nested loop solution
static int[] NextLarger_Loop(int[] aa)
{
int[] result = new int[aa.Length];
for(int i = 0; i < aa.Length; i++)
{
result[i] = -1;
for(int j = 1; j < aa.Length - i; j++)
{
if(aa[i] < aa[i+j])
{
result[i] = j;
break;
}
}
}
return result;
}
//stack based solution
static int[] NextLarger_Stack(int[] aa)
{
int[] result = new int[aa.Length];
var index = new Stack<int>();
for(int i = 0; i < aa.Length; i++)
{
result[i] = -1;
while(index.Count > 0 && aa[i] > aa[index.Peek()])
{
var top = index.Pop();
result[top] = i - top;
}
index.Push(i);
}
return result;
}
static void WriteLine(int[] a)
{
Console.WriteLine(String.Join(", ", a));
}
static bool VerboseVerify(int[] expected, int[] actual, string testName)
{
var result = Verify(expected, actual);
if(result)
Console.WriteLine(testName + " results match expected:");
else
Console.WriteLine(testName + " results don't match expected:");
WriteLine(actual);
return result;
}
static bool Verify(int[] expected, int[] actual)
{
for(int i = 0; i < expected.Length; i++)
{
if(expected[i] != actual[i])
return false;
}
return true;
}
}