Skip to content

Added C# solutions for Chapter 2 (Hash Maps and Sets) #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions csharp/Hash Maps and Sets/GeometricSequenceTriplets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Solution
{
public int GeometricSequenceTriplets(int[] nums, int r)
{
// The default value of 0 is returned when accessing a key that
// doesn’t exist in the hash map.
Dictionary<int, int> leftMap = new Dictionary<int, int>();
Dictionary<int, int> rightMap = new Dictionary<int, int>();
int count = 0;
// Populate 'right_map' with the frequency of each element in the array.
foreach (int x in nums)
{
rightMap[x] = rightMap.GetValueOrDefault(x) + 1;
}
// Search for geometric triplets that have x as the center.
foreach (int x in nums)
{
// Decrement the frequency of x in 'right_map' since x is now being
// processed and is no longer to the right.
rightMap[x] = rightMap.GetValueOrDefault(x) - 1;
if (x % r == 0)
{
count += leftMap.GetValueOrDefault(x / r) * rightMap.GetValueOrDefault(x * r);
}
// Increment the frequency of x in 'left_map' since it'll be a part of the
// left side of the array once we iterate to the next value of x.
leftMap[x] = leftMap.GetValueOrDefault(x) + 1;
}

return count;
}
}
31 changes: 31 additions & 0 deletions csharp/Hash Maps and Sets/LongestChainOfConsecutiveNumbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Solution
{
public int LongestChainOfConsecutiveNumbers(int[] nums)
{
if (nums == null || nums.Length == 0)
{
return 0;
}
HashSet<int> numSet = new HashSet<int>(nums);
int longestChain = 0;
foreach (int num in numSet)
{
// If the current number is the smallest number in its chain, search for
// the length of its chain.
if (!numSet.Contains(num - 1))
{
int currentNum = num;
int currentChain = 1;
// Continue to find the next consecutive numbers in the chain.
while (numSet.Contains(currentNum + 1))
{
currentNum++;
currentChain++;
}
longestChain = Math.Max(longestChain, currentChain);
}
}

return longestChain;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Solution
{
public int LongestChainOfConsecutiveNumbersBruteForce(int[] nums)
{
if (nums == null || nums.Length == 0)
{
return 0;
}
int longestChain = 0;
// Look for chains of consecutive numbers that start from each number.
foreach (int num in nums)
{
int currentNum = num;
int currentChain = 1;
// Continue to find the next consecutive numbers in the chain.
while (nums.Contains(currentNum + 1))
{
currentNum++;
currentChain++;
}
longestChain = Math.Max(longestChain, currentChain);
}

return longestChain;
}
}
18 changes: 18 additions & 0 deletions csharp/Hash Maps and Sets/PairSumUnsorted.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Solution
{
public int[] PairSumUnsorted(int[] nums, int target)
{
Dictionary<int, int> hashmap = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)
{
if (hashmap.ContainsKey(target - nums[i]))
{
return [hashmap[target - nums[i]], i];
}

hashmap[nums[i]] = i;
}

return [];
}
}
24 changes: 24 additions & 0 deletions csharp/Hash Maps and Sets/PairSumUnsortedTwoPass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution
{
public int[] PairSumUnsortedTwoPass(int[] nums, int target)
{
Dictionary<int, int> numMap = new Dictionary<int, int>();
// First pass: Populate the hash map with each number and its
// index
for (int i = 0; i < nums.Length; i++)
{
numMap[nums[i]] = i;
}
// Second pass: Check for each number's complement in the hash map.
for (int i = 0; i < nums.Length; i++)
{
int complement = target - nums[i];
if (numMap.ContainsKey(complement) && numMap[complement] != i)
{
return [i, numMap[complement]];
}
}

return [];
}
}
61 changes: 61 additions & 0 deletions csharp/Hash Maps and Sets/VerifySudokuBoard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
public class Solution
{
public bool VerifySudokuBoard(int[][] board)
{
// Create hash sets for each row, column, and subgrid to keep
// track of numbers previously seen on any given row, column, or
// subgrid.
HashSet<int>[] rowSets = new HashSet<int>[9];
HashSet<int>[] columnSets = new HashSet<int>[9];
HashSet<int>[,] subgridSets = new HashSet<int>[3, 3];

// Initializing row and column sets.
for (int i = 0; i < 9; i++)
{
rowSets[i] = new HashSet<int>();
columnSets[i] = new HashSet<int>();
}

// Initializing the subgrid sets.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
subgridSets[i, j] = new HashSet<int>();
}
}

for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 9; c++)
{
int num = board[r][c];
if (num == 0)
{
continue;
}
// Check if 'num' has been seen in the current row,
// column, or subgrid.
if (rowSets[r].Contains(num))
{
return false;
}
if (columnSets[c].Contains(num))
{
return false;
}
if (subgridSets[r / 3, c / 3].Contains(num))
{
return false;
}
// If we passed the above checks, mark this value as seen
// by adding it to its corresponding hash sets.
rowSets[r].Add(num);
columnSets[c].Add(num);
subgridSets[r / 3, c / 3].Add(num);
}
}

return true;
}
}
75 changes: 75 additions & 0 deletions csharp/Hash Maps and Sets/ZeroStriping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
public class Solution
{
public void ZeroStriping(int[][] matrix)
{
if (matrix == null || matrix.Length == 0 || matrix[0] == null || matrix[0].Length == 0)
{
return;
}
int m = matrix.Length, n = matrix[0].Length;
// Check if the first row initially contains a zero.
bool firstRowHasZero = false;
for (int c = 0; c < n; c++)
{
if (matrix[0][c] == 0)
{
firstRowHasZero = true;
break;
}
}
// Check if the first column initially contains a zero.
bool firstColHasZero = false;
for (int r = 0; r < m; r++)
{
if (matrix[r][0] == 0)
{
firstColHasZero = true;
break;
}
}
// Use the first row and column as markers. If an element in the
// submatrix is zero, mark its corresponding row and column in the
// first row and column as 0.
for (int r = 1; r < m; r++)
{
for (int c = 1; c < n; c++)
{
if (matrix[r][c] == 0)
{
matrix[0][c] = 0;
matrix[r][0] = 0;
}
}
}
// Update the submatrix using the markers in the first row and
// column.
for (int r = 1; r < m; r++)
{
for (int c = 1; c < n; c++)
{
if (matrix[0][c] == 0 || matrix[r][0] == 0)
{
matrix[r][c] = 0;
}
}
}
// If the first row had a zero initially, set all elements in the
// first row to zero.
if (firstRowHasZero)
{
for (int c = 0; c < n; c++)
{
matrix[0][c] = 0;
}
}
// If the first column had a zero initially, set all elements in
// the first column to zero.
if (firstColHasZero)
{
for (int r = 0; r < m; r++)
{
matrix[r][0] = 0;
}
}
}
}
39 changes: 39 additions & 0 deletions csharp/Hash Maps and Sets/ZeroStripingHashSets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class Solution
{
public void ZeroStripingHashSets(int[][] matrix)
{
if (matrix == null || matrix.Length == 0 || matrix[0] == null || matrix[0].Length == 0)
{
return;
}
int m = matrix.Length, n = matrix[0].Length;
HashSet<int> zeroRows = new HashSet<int>();
HashSet<int> zeroCols = new HashSet<int>();
// Pass 1: Traverse through the matrix to identify the rows and
// columns containing zeros and store their indexes in the
// appropriate hash sets.
for (int r = 0; r < m; r++)
{
for (int c = 0; c < n; c++)
{
if (matrix[r][c] == 0)
{
zeroRows.Add(r);
zeroCols.Add(c);
}
}
}
// Pass 2: Set any cell in the matrix to zero if its row index is
// in 'zero_rows' or its column index is in 'zero_cols'.
for (int r = 0; r < m; r++)
{
for (int c = 0; c < n; c++)
{
if (zeroRows.Contains(r) || zeroCols.Contains(c))
{
matrix[r][c] = 0;
}
}
}
}
}