public void Validate2() { var sut = new FreqStack(); sut.Push(4); sut.Push(0); sut.Push(9); sut.Push(3); sut.Push(4); sut.Push(2); Assert.AreEqual(4, sut.Pop()); sut.Push(6); Assert.AreEqual(6, sut.Pop()); sut.Push(1); Assert.AreEqual(1, sut.Pop()); sut.Push(1); Assert.AreEqual(1, sut.Pop()); sut.Push(4); Assert.AreEqual(4, sut.Pop()); Assert.AreEqual(2, sut.Pop()); Assert.AreEqual(3, sut.Pop()); Assert.AreEqual(9, sut.Pop()); Assert.AreEqual(0, sut.Pop()); Assert.AreEqual(4, sut.Pop()); }
/* * ["FreqStack","push","push","push","push","push","push","pop","push","pop","push","pop","push","pop","push","pop","pop","pop","pop","pop","pop"] * [[],[4],[0],[9],[3],[4],[2],[],[6],[],[1],[],[1],[],[4],[],[],[],[],[],[]] */ public static void Test() { var stack = new FreqStack(); var arr = new [] { 4, 0, 9, 3, 4, 2 }; foreach (var elem in arr) { stack.Push(elem); } stack.Pop(); stack.Push(6); stack.Pop(); stack.Push(1); stack.Pop(); stack.Push(1); stack.Pop(); stack.Push(4); for (var i = 0; i < 6; i++) { stack.Pop(); } }
public void Test2() { var freqStack = new FreqStack(); freqStack.Push(4); // The stack is [4] freqStack.Push(0); // The stack is [4,0] freqStack.Push(9); // The stack is [4,0,9] freqStack.Push(3); // The stack is [4,0,9,3] freqStack.Push(4); // The stack is [4,0,9,3,4] freqStack.Push(2); // The stack is [4,0,9,3,4,2] freqStack.Pop().Should().Be(4); freqStack.Push(6); freqStack.Pop().Should().Be(6); freqStack.Push(1); freqStack.Pop().Should().Be(1); freqStack.Push(1); freqStack.Pop().Should().Be(1); freqStack.Push(4); freqStack.Pop().Should().Be(4); freqStack.Pop().Should().Be(2); freqStack.Pop().Should().Be(3); freqStack.Pop().Should().Be(9); freqStack.Pop().Should().Be(0); }
public void FreqStackTest() { FreqStack stack = new FreqStack(); stack.Push(5); stack.Push(7); stack.Push(5); stack.Push(7); stack.Push(4); stack.Push(5); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop()); Console.WriteLine(stack.Pop()); }
public void Test1() { var freqStack = new FreqStack(); freqStack.Push(5); // The stack is [5] freqStack.Push(7); // The stack is [5,7] freqStack.Push(5); // The stack is [5,7,5] freqStack.Push(7); // The stack is [5,7,5,7] freqStack.Push(4); // The stack is [5,7,5,7,4] freqStack.Push(5); // The stack is [5,7,5,7,4,5] freqStack.Pop().Should().Be(5); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4]. freqStack.Pop().Should().Be(7); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4]. freqStack.Pop().Should().Be(5); // return 5, as 5 is the most frequent. The stack becomes [5,7,4]. freqStack.Pop().Should().Be(4); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7]. }
private static void TestFreqStack() { FreqStack instance = new FreqStack(); instance.Push(5); instance.Push(7); instance.Push(5); instance.Push(7); instance.Push(4); instance.Push(5); Console.WriteLine(instance.Pop()); Console.WriteLine(instance.Pop()); Console.WriteLine(instance.Pop()); Console.WriteLine(instance.Pop()); }
//Accepted-LcHard-LcSol-T:O(1)-S:O(n) https://leetcode.com/problems/maximum-frequency-stack/ public void FrequencyStack() { FreqStack freqStack = new FreqStack(); freqStack.Push(5); freqStack.Push(7); freqStack.Push(5); freqStack.Push(7); freqStack.Push(4); freqStack.Push(5); for (int idx = 0; idx < 4; idx++) { Console.WriteLine(freqStack.Pop()); } }
public void Validate1() { var sut = new FreqStack(); sut.Push(5); sut.Push(7); sut.Push(5); sut.Push(7); sut.Push(4); sut.Push(5); Assert.AreEqual(5, sut.Pop()); Assert.AreEqual(7, sut.Pop()); Assert.AreEqual(5, sut.Pop()); Assert.AreEqual(4, sut.Pop()); Assert.AreEqual(7, sut.Pop()); Assert.AreEqual(5, sut.Pop()); }
static void Main(string[] args) { FreqStack s = new FreqStack(); int rs; s.Push(5); s.Push(7); s.Push(5); s.Push(7); s.Push(4); s.Push(5); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {5}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {7}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {5}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {4}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {7}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {5}"); Console.WriteLine("Phase 2"); s.Push(4); s.Push(0); s.Push(9); s.Push(3); s.Push(4); s.Push(2); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {4}"); s.Push(6); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {6}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {2}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {3}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {9}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {0}"); rs = s.Pop(); Console.WriteLine($"Result: {rs} - Expect: {4}"); }