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/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; + } + } +} + 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; + } + } +} + 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; + } + } +} + 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; + } + } +} + 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/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; + } + } +} + 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/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/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(); + } + } +} + 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 }; + } + } +} + 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; + } + } +} + 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/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; + } + + } +} + 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/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 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