From 87ae406fd79d7d07de273b671e017f720334fa9f Mon Sep 17 00:00:00 2001 From: basigu Date: Sat, 7 May 2022 20:52:24 -0700 Subject: [PATCH 01/11] Add files via upload Added c# solutions for 1,49,70,217,238,242,347 and 746 --- csharp/1-Two-Sum.cs | 22 +++++++++++++ csharp/217-Contains-Duplicate.cs | 19 ++++++++++++ csharp/238-Product-Except-Self.cs | 43 ++++++++++++++++++++++++++ csharp/242-Valid-Anagram.cs | 43 ++++++++++++++++++++++++++ csharp/347-Top-K-Frequent-Elements.cs | 43 ++++++++++++++++++++++++++ csharp/49-Group-Anagrams.cs | 36 +++++++++++++++++++++ csharp/70-Climbing-Stairs.cs | 16 ++++++++++ csharp/746-Min-Cost-Climbing-Stairs.cs | 17 ++++++++++ 8 files changed, 239 insertions(+) create mode 100644 csharp/1-Two-Sum.cs create mode 100644 csharp/217-Contains-Duplicate.cs create mode 100644 csharp/238-Product-Except-Self.cs create mode 100644 csharp/242-Valid-Anagram.cs create mode 100644 csharp/347-Top-K-Frequent-Elements.cs create mode 100644 csharp/49-Group-Anagrams.cs create mode 100644 csharp/70-Climbing-Stairs.cs create mode 100644 csharp/746-Min-Cost-Climbing-Stairs.cs diff --git a/csharp/1-Two-Sum.cs b/csharp/1-Two-Sum.cs new file mode 100644 index 000000000..454871bab --- /dev/null +++ b/csharp/1-Two-Sum.cs @@ -0,0 +1,22 @@ +public class Solution +{ + public int[] TwoSum(int[] nums, int target) + { + var numToIndex = new Dictionary(); + for (var i = 0; i < nums.Length; i++) + { + var diff = target - nums[i]; + if (numToIndex.ContainsKey(diff)) + { + return new int[] { i, numToIndex[diff] }; + } + else + { + numToIndex[nums[i]] = i; + } + } + + return new int[0]; + } +} + diff --git a/csharp/217-Contains-Duplicate.cs b/csharp/217-Contains-Duplicate.cs new file mode 100644 index 000000000..695b6a646 --- /dev/null +++ b/csharp/217-Contains-Duplicate.cs @@ -0,0 +1,19 @@ +public class Solution +{ + public bool ContainsDuplicate(int[] nums) + { + var hashSet = new HashSet(); + foreach (var num in nums) + { + if (hashSet.Contains(num)) + { + return true; + } + else + { + hashSet.Add(num); + } + } + return false; + } +} \ No newline at end of file diff --git a/csharp/238-Product-Except-Self.cs b/csharp/238-Product-Except-Self.cs new file mode 100644 index 000000000..724c62746 --- /dev/null +++ b/csharp/238-Product-Except-Self.cs @@ -0,0 +1,43 @@ +public class Solution5 +{ + public int[] TopKFrequent(int[] nums, int k) + { + var topK = new int[k]; + + var numToCount = new Dictionary(); + + foreach (var num in nums) + { + if (numToCount.ContainsKey(num)) + { + numToCount[num]++; + } + else + { + numToCount[num] = 1; + } + } + + PriorityQueue priorityQueue = new(new ElementComparer()); + + foreach (var kv in numToCount) + { + priorityQueue.Enqueue(kv.Key, kv.Value); + } + + for (var i = 0; i < k; i++) + { + topK[i] = priorityQueue.Dequeue(); + } + + return topK; + } +} + +public class ElementComparer : IComparer +{ + public int Compare(int x, int y) + { + return y - x; + } +} diff --git a/csharp/242-Valid-Anagram.cs b/csharp/242-Valid-Anagram.cs new file mode 100644 index 000000000..78d929e76 --- /dev/null +++ b/csharp/242-Valid-Anagram.cs @@ -0,0 +1,43 @@ +public class Solution +{ + public bool IsAnagram(string s, string t) + { + var sStrToCount = CreateMap(s); + var tStrToCount = CreateMap(t); + + if (sStrToCount.Keys.Count != tStrToCount.Keys.Count) + { + return false; + } + + foreach (var key in sStrToCount.Keys) + { + if (!tStrToCount.ContainsKey(key)) + { + return false; + } + if (sStrToCount[key] != tStrToCount[key]) + { + return false; + } + } + return true; + } + + Dictionary CreateMap(string str) + { + var dict = new Dictionary(); + foreach (var ch in str) + { + if (dict.ContainsKey(ch)) + { + dict[ch]++; + } + else + { + dict[ch] = 1; + } + } + return dict; + } +} \ No newline at end of file diff --git a/csharp/347-Top-K-Frequent-Elements.cs b/csharp/347-Top-K-Frequent-Elements.cs new file mode 100644 index 000000000..6cb421a2f --- /dev/null +++ b/csharp/347-Top-K-Frequent-Elements.cs @@ -0,0 +1,43 @@ +public class Solution +{ + public int[] TopKFrequent(int[] nums, int k) + { + var topK = new int[k]; + + var numToCount = new Dictionary(); + + foreach (var num in nums) + { + if (numToCount.ContainsKey(num)) + { + numToCount[num]++; + } + else + { + numToCount[num] = 1; + } + } + + PriorityQueue priorityQueue = new(new ElementComparer()); + + foreach (var kv in numToCount) + { + priorityQueue.Enqueue(kv.Key, kv.Value); + } + + for (var i = 0; i < k; i++) + { + topK[i] = priorityQueue.Dequeue(); + } + + return topK; + } +} + +public class ElementComparer : IComparer +{ + public int Compare(int x, int y) + { + return y - x; + } +} diff --git a/csharp/49-Group-Anagrams.cs b/csharp/49-Group-Anagrams.cs new file mode 100644 index 000000000..0243392f3 --- /dev/null +++ b/csharp/49-Group-Anagrams.cs @@ -0,0 +1,36 @@ +public class Solution +{ + public IList> GroupAnagrams(string[] strs) + { + IList> result = new List>(); + if (strs == null || strs.Length == 0) + { + return result; + } + + var encodedStrToValueList = new Dictionary>(); + foreach (var str in strs) + { + var arr = new int[26]; + foreach (var c in str) + { + arr[c - 'a']++; + } + var encodedStr = string.Join('#', arr); + if (encodedStrToValueList.ContainsKey(encodedStr)) + { + encodedStrToValueList[encodedStr].Add(str); + } + else + { + encodedStrToValueList[encodedStr] = new List() { str }; + } + } + + foreach (var kv in encodedStrToValueList) + { + result.Add(kv.Value); + } + return result; + } +} \ No newline at end of file diff --git a/csharp/70-Climbing-Stairs.cs b/csharp/70-Climbing-Stairs.cs new file mode 100644 index 000000000..6d2784a28 --- /dev/null +++ b/csharp/70-Climbing-Stairs.cs @@ -0,0 +1,16 @@ +public class Solution +{ + public int ClimbStairs(int n) + { + var oneStep = 1; + var twoStep = 1; + + for (var i = 0; i < n - 1; i++) + { + var temp = oneStep; + oneStep = oneStep + twoStep; + twoStep = temp; + } + return oneStep; + } +} \ No newline at end of file diff --git a/csharp/746-Min-Cost-Climbing-Stairs.cs b/csharp/746-Min-Cost-Climbing-Stairs.cs new file mode 100644 index 000000000..46fa6d469 --- /dev/null +++ b/csharp/746-Min-Cost-Climbing-Stairs.cs @@ -0,0 +1,17 @@ +public class Solution +{ + public int MinCostClimbingStairs(int[] cost) + { + int[] minCost = new int[cost.Length + 1]; + minCost[minCost.Length - 1] = 0; + minCost[minCost.Length - 2] = cost[cost.Length - 1]; + + for (var i = minCost.Length - 3; i >= 0; i--) + { + var oneStep = cost[i] + minCost[i + 1]; + var twoStep = cost[i] + minCost[i + 2]; + minCost[i] = Math.Min(oneStep, twoStep); + } + return Math.Min(minCost[0], minCost[1]); + } +} \ No newline at end of file From 03ec11a632219ae52d222190256c3a0f7f1a19c0 Mon Sep 17 00:00:00 2001 From: basigu Date: Sun, 8 May 2022 19:14:43 -0700 Subject: [PATCH 02/11] Added 36 valid sudoku solution --- csharp/36-Valid-Sudoku.cs | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 csharp/36-Valid-Sudoku.cs diff --git a/csharp/36-Valid-Sudoku.cs b/csharp/36-Valid-Sudoku.cs new file mode 100644 index 000000000..5e10f7f62 --- /dev/null +++ b/csharp/36-Valid-Sudoku.cs @@ -0,0 +1,62 @@ +public class Solution +{ + public bool IsValidSudoku(char[][] board) + { + HashSet[] rows = InitializeHashSet(board.Length); + HashSet[] cols = InitializeHashSet(board.Length); + HashSet[][] squares = InitializeSquaresHashSet(board.Length / 3); + + for (var row = 0; row < board.Length; row++) + { + for (var col = 0; col < board.Length; col++) + { + var value = board[row][col]; + if (value == '.') + { + continue; + } + + var intValue = value - '0'; + if (rows[row].Contains(intValue) + || cols[col].Contains(intValue) + || squares[row / 3][col / 3].Contains(intValue)) + { + return false; + } + else + { + rows[row].Add(intValue); + cols[col].Add(intValue); + squares[row / 3][col / 3].Add(intValue); + } + } + } + return true; + } + + private HashSet[] InitializeHashSet(int length) + { + var collection = new HashSet[length]; + for (var i = 0; i < length; i++) + { + collection[i] = new HashSet(); + } + return collection; + } + + private HashSet[][] InitializeSquaresHashSet(int length) + { + var collection = new HashSet[length][]; + for (var col = 0; col < length; col++) + { + var row = new HashSet[length]; + for (var i = 0; i < length; i++) + { + row[i] = new HashSet(); + } + collection[col] = row; + + } + return collection; + } +} \ No newline at end of file From df90bd64a43e02f9e6ad63f6deaa22da8c654afb Mon Sep 17 00:00:00 2001 From: basigu Date: Mon, 9 May 2022 20:02:06 -0700 Subject: [PATCH 03/11] Problem# 271 and 128 --- csharp/128-Longest-Consecutive-Sequence.cs | 28 ++++++++++++++ csharp/271-Encode-Decode-Strings.cs | 44 ++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 csharp/128-Longest-Consecutive-Sequence.cs create mode 100644 csharp/271-Encode-Decode-Strings.cs diff --git a/csharp/128-Longest-Consecutive-Sequence.cs b/csharp/128-Longest-Consecutive-Sequence.cs new file mode 100644 index 000000000..aebc01507 --- /dev/null +++ b/csharp/128-Longest-Consecutive-Sequence.cs @@ -0,0 +1,28 @@ +public class Solution12 +{ + public int LongestConsecutive(int[] nums) + { + var numSet = new HashSet(); + var longest = 0; + + foreach(var num in nums) + { + numSet.Add(num); + } + + foreach(var num in nums) + { + var length = 0; + if (!numSet.Contains(num - 1)) + { + while (numSet.Contains(num + length)) + { + length += 1; + } + + longest = Math.Max(longest, length); + } + } + return longest; + } +} \ No newline at end of file diff --git a/csharp/271-Encode-Decode-Strings.cs b/csharp/271-Encode-Decode-Strings.cs new file mode 100644 index 000000000..af009b185 --- /dev/null +++ b/csharp/271-Encode-Decode-Strings.cs @@ -0,0 +1,44 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + + // Encodes a list of strings to a single string. + public string Encode(IList strs) + { + var result = string.Empty; + foreach(var str in strs) + { + result += $"{str.Length}#{str}"; + } + return result; + } + + // Decodes a single string to a list of strings. + public IList Decode(string s) + { + IList result = new List(); + var i = 0; + while(i < s.Length) + { + var j = i; + while(s[j] != '#') + { + j++; + } + + var numStr = s.Substring(i, j - i); + var num = int.Parse(numStr); + + var word = s.Substring(j+1, num); + result.Add(word); + + i = j + 1 + num; + } + return result; + } + + } +} + From b2fe51640fb0b8e08dd68d0616515c5cc3cd0bf6 Mon Sep 17 00:00:00 2001 From: basigu Date: Tue, 10 May 2022 21:00:49 -0700 Subject: [PATCH 04/11] Added solution for 125 Valid Palindrome --- csharp/125-Valid-Palindrome.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 csharp/125-Valid-Palindrome.cs diff --git a/csharp/125-Valid-Palindrome.cs b/csharp/125-Valid-Palindrome.cs new file mode 100644 index 000000000..57ef44dc0 --- /dev/null +++ b/csharp/125-Valid-Palindrome.cs @@ -0,0 +1,31 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public bool IsPalindrome(string s) + { + var left = 0; + var right = s.Length - 1; + + while(left < right) + { + while (left < right && !char.IsLetterOrDigit(s[left])) left++; + while (left < right && !char.IsLetterOrDigit(s[right])) right--; + + if (char.ToLower(s[left]) != char.ToLower(s[right])) + { + return false; + } + else + { + left++; + right--; + } + + } + return true; + } + } +} + From f34eb1ea87f9f35dc5eccf9e1dd28f92ac25eb93 Mon Sep 17 00:00:00 2001 From: basigu Date: Wed, 11 May 2022 20:47:04 -0700 Subject: [PATCH 05/11] Problem #167 and 15 solutions with two pointers --- csharp/15-3-Sum.cs | 57 ++++++++++++++++++++++++++++++++++++++++ csharp/167-Two-Sum-II.cs | 30 +++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 csharp/15-3-Sum.cs create mode 100644 csharp/167-Two-Sum-II.cs diff --git a/csharp/15-3-Sum.cs b/csharp/15-3-Sum.cs new file mode 100644 index 000000000..aeaf1e4db --- /dev/null +++ b/csharp/15-3-Sum.cs @@ -0,0 +1,57 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public IList> ThreeSum(int[] nums) + { + Array.Sort(nums); + + IList> triplet = new List>(); + + for(var i = 0; i < nums.Length; i++) + { + if(i > 0 && nums[i] == nums[i - 1]) + { + continue; + } + Search(-nums[i], i, nums.Length - 1, nums, triplet); + } + return triplet; + } + + private void Search(int target, int left, int right, int[] nums, IList> triplet) + { + while(left < right) + { + var sum = nums[left] + nums[right]; + if(sum == target) + { + triplet.Add(new List { -target, nums[left], nums[right] }); + left++; + right--; + + while (left < right && nums[left] == nums[left - 1]) + { + left++; + } + + while (left < right && nums[right] == nums[right + 1]) + { + right--; + } + } + else if(sum < target) + { + left++; + + } + else + { + right--; + } + } + } + } +} + diff --git a/csharp/167-Two-Sum-II.cs b/csharp/167-Two-Sum-II.cs new file mode 100644 index 000000000..1cf674a74 --- /dev/null +++ b/csharp/167-Two-Sum-II.cs @@ -0,0 +1,30 @@ +using System; +namespace AlgoPractice +{ + public class Solution17 + { + public int[] TwoSum(int[] numbers, int target) + { + var left = 0; + var right = numbers.Length - 1; + + while(left < right) + { + if(numbers[left] + numbers[right] == target) + { + return new int[] { left + 1, right + 1 }; + } + else if(numbers[left] + numbers[right] < target) + { + left++; + } + else + { + right--; + } + } + return new int[] { -1, -1 }; + } + } +} + From 3fe1f13f1dbef6b5765c268a66a54f5c613925e0 Mon Sep 17 00:00:00 2001 From: basigu Date: Thu, 12 May 2022 20:47:21 -0700 Subject: [PATCH 06/11] #11(Container with water) and 42(Trapping rain) --- 11-Container-with-most-water.cs | 31 +++++++++++++++++++++++ 42-Trapping-Rain-Water.cs | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 11-Container-with-most-water.cs create mode 100644 42-Trapping-Rain-Water.cs diff --git a/11-Container-with-most-water.cs b/11-Container-with-most-water.cs new file mode 100644 index 000000000..9dac894c4 --- /dev/null +++ b/11-Container-with-most-water.cs @@ -0,0 +1,31 @@ +using System; +namespace AlgoPractice +{ + public class Solution20 + { + public int MaxArea(int[] height) + { + var left = 0; + var right = height.Length - 1; + var maxArea = 0; + + while(left < right) + { + var area = (right - left) * Math.Min(height[left], height[right]); + maxArea = Math.Max(maxArea, area); + + if(height[left] < height[right]) + { + left++; + } + else + { + right--; + } + } + + return maxArea; + } + } +} + diff --git a/42-Trapping-Rain-Water.cs b/42-Trapping-Rain-Water.cs new file mode 100644 index 000000000..297ac94f1 --- /dev/null +++ b/42-Trapping-Rain-Water.cs @@ -0,0 +1,44 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public int Trap(int[] height) + { + var left = 0; + var right = height.Length - 1; + + var maxLeft = height[left]; + var maxRight = height[right]; + + var result = 0; + + while(left < right) + { + if(maxLeft < maxRight) + { + left++; + var currentResult = maxLeft - height[left]; + if(currentResult > 0) + { + result += currentResult; + } + maxLeft = Math.Max(maxLeft, height[left]); + } + else + { + right--; + var currentResult = maxRight - height[right]; + if(currentResult > 0) + { + result += currentResult; + } + maxRight = Math.Max(maxRight, height[right]); + } + + } + return result; + } + } +} + From 0beeecd637062021ca6ad805ddc6cd020b3daa0a Mon Sep 17 00:00:00 2001 From: basigu Date: Sun, 15 May 2022 16:02:23 -0700 Subject: [PATCH 07/11] 198 and 213 house robber. --- 198-House-Robber.cs | 29 +++++++++++++++++++++++++++++ 213-House-Robber-II.cs | 28 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 198-House-Robber.cs create mode 100644 213-House-Robber-II.cs diff --git a/198-House-Robber.cs b/198-House-Robber.cs new file mode 100644 index 000000000..7e91f26c5 --- /dev/null +++ b/198-House-Robber.cs @@ -0,0 +1,29 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public int Rob(int[] nums) + { + if (nums == null || nums.Length == 0) + { + return 0; + } + else if (nums.Length == 1) + { + return nums[0]; + } + + var rob1 = 0; + var rob2 = 0; + foreach (var n in nums) + { + var temp = Math.Max(rob1 + n, rob2); + rob1 = rob2; + rob2 = temp; + } + return rob2; + } + } +} + diff --git a/213-House-Robber-II.cs b/213-House-Robber-II.cs new file mode 100644 index 000000000..3260e8076 --- /dev/null +++ b/213-House-Robber-II.cs @@ -0,0 +1,28 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public int Rob(int[] nums) + { + var startFromHouse1 = RobHelper(nums, 0, nums.Length - 2); + var startFromHouse2 = RobHelper(nums, 1, nums.Length - 1); + var result = Math.Max(startFromHouse1, startFromHouse2); + return result; + } + + public int RobHelper(int[] num, int start, int end) + { + var rob1 = 0; + var rob2 = 0; + for(var i = start; i<= end; i++) + { + var temp = Math.Max(rob1 + num[i], rob2); + rob1 = rob2; + rob2 = temp; + } + return rob2; + } + } +} + From 2eb4f3ac9326a4b53e9c9f079c192371561a7e26 Mon Sep 17 00:00:00 2001 From: basigu Date: Sun, 15 May 2022 16:51:41 -0700 Subject: [PATCH 08/11] 121 best time to buy stock --- 121-Best-Time-Stock.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 121-Best-Time-Stock.cs diff --git a/121-Best-Time-Stock.cs b/121-Best-Time-Stock.cs new file mode 100644 index 000000000..4f6c28c24 --- /dev/null +++ b/121-Best-Time-Stock.cs @@ -0,0 +1,31 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public int MaxProfit(int[] prices) + { + var maxProfit = 0; + + var left = 0; + var right = 1; + + while (right < prices.Length) + { + var profit = prices[right] - prices[left]; + if(profit > 0) + { + maxProfit = Math.Max(profit, maxProfit); + } + else + { + left = right; + } + right++; + } + + return maxProfit; + } + } +} + From 1ceb60fa27817a3e60e80536d8e16e5b260caa9c Mon Sep 17 00:00:00 2001 From: basigu Date: Fri, 20 May 2022 19:47:29 -0700 Subject: [PATCH 09/11] #3 Longest substring --- 3-Longest-Substring.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 3-Longest-Substring.cs diff --git a/3-Longest-Substring.cs b/3-Longest-Substring.cs new file mode 100644 index 000000000..feed876f8 --- /dev/null +++ b/3-Longest-Substring.cs @@ -0,0 +1,28 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public int LengthOfLongestSubstring(string s) + { + var set = new HashSet(); + var maxLength = 0; + var left = 0; + var right = 0; + while(right < s.Length) + { + var currentChar = s[right]; + while (set.Contains(currentChar) && left < s.Length) + { + set.Remove(s[left++]); + } + set.Add(currentChar); + right++; + var length = right - left; + maxLength = Math.Max(length, maxLength); + } + return maxLength; + } + } +} + From 5bec4b6cf1ed30316808418aae7c1c93730b9a0c Mon Sep 17 00:00:00 2001 From: basigu Date: Tue, 31 May 2022 20:15:59 -0700 Subject: [PATCH 10/11] 20 Valid Parentheses --- csharp/20-Valid-Parentheses.cs | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 csharp/20-Valid-Parentheses.cs diff --git a/csharp/20-Valid-Parentheses.cs b/csharp/20-Valid-Parentheses.cs new file mode 100644 index 000000000..bd08bcf39 --- /dev/null +++ b/csharp/20-Valid-Parentheses.cs @@ -0,0 +1,35 @@ +using System; +namespace AlgoPractice +{ + public class Solution + { + public bool IsValid(string s) + { + var map = new Dictionary + { + [')'] = '(', + ['}'] = '{', + [']'] = '[' + }; + + var stack = new Stack(); + foreach(char c in s) + { + if (map.ContainsKey(c)) + { + var matchingPar = stack.Count == 0 ? '#' : stack.Pop(); + if(map[c] != matchingPar) + { + return false; + } + } + else + { + stack.Push(c); + } + } + return stack.Count == 0; + } + } +} + From 98b7152cbf90f8a67cf1b0d377713bc55a7e0e38 Mon Sep 17 00:00:00 2001 From: basigu Date: Tue, 14 Jun 2022 19:44:50 -0700 Subject: [PATCH 11/11] 155 min stack --- csharp/155-Min-Stack.cs | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 csharp/155-Min-Stack.cs diff --git a/csharp/155-Min-Stack.cs b/csharp/155-Min-Stack.cs new file mode 100644 index 000000000..44b847465 --- /dev/null +++ b/csharp/155-Min-Stack.cs @@ -0,0 +1,45 @@ +using System; +namespace AlgoPractice +{ + public class MinStack + { + private Stack _stack; + private Stack _minStack; + + public MinStack() + { + _stack = new Stack(); + _minStack = new Stack(); + } + + void Push(int val) + { + _stack.Push(val); + if(val < _minStack.Peek()) + { + _minStack.Push(val); + } + } + + public void Pop() + { + if (_stack.Count() == 0) return; + var val = _stack.Pop(); + if(val == _minStack.Peek()) + { + _minStack.Pop(); + } + } + + public int Top() + { + return _stack.Count() == 0 ? -1 : _stack.Peek(); + } + + public int GetMin() + { + return _minStack.Count() == 0 ? -1 : _minStack.Peek(); + } + } +} +