-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinStack.cs
64 lines (56 loc) · 1.72 KB
/
MinStack.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 155. Min Stack
/// Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
/// + push(x) : Push element x onto stack
/// + pop() : Remove the element on top of the stack
/// + top() : Get the top element
/// + getMin() : Retrieve the minimum element in the stack
/// </summary>
public class MinStack : Solution
{
private List<int> _data;
public MinStack()
{
_data = new List<int>();
}
public void Push(int x)
{
_data.Add(x);
}
public void Pop()
{
_data.RemoveAt(_data.Count - 1);
}
public int Top()
{
return _data[_data.Count - 1];
}
public int GetMin()
{
return _data.Min();
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
MinStack obj = new MinStack();
obj.Push(2);
obj.Push(-3);
Console.WriteLine($"155. Min Stack\n" +
$"MinStack obj = new MinStack(); \n" +
$"obj.Push(2)\n" +
$"obj.Push(-3)\n" +
$"obj.Top() : {obj.Top()}");
obj.Pop();
Console.WriteLine($"obj.Pop() \n" +
$"obj.GetMin() : {obj.GetMin()}");
watch.Stop();
Console.WriteLine($"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}