Skip to content

Commit 269584d

Browse files
committed
Added C# solutions for Chapter 2: Hash Maps and Sets.
1 parent d03d5c7 commit 269584d

8 files changed

+306
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Solution
2+
{
3+
public int GeometricSequenceTriplets(int[] nums, int r)
4+
{
5+
// The default value of 0 is returned when accessing a key that
6+
// doesn’t exist in the hash map.
7+
Dictionary<int, int> leftMap = new Dictionary<int, int>();
8+
Dictionary<int, int> rightMap = new Dictionary<int, int>();
9+
int count = 0;
10+
// Populate 'right_map' with the frequency of each element in the array.
11+
foreach (int x in nums)
12+
{
13+
rightMap[x] = rightMap.GetValueOrDefault(x) + 1;
14+
}
15+
// Search for geometric triplets that have x as the center.
16+
foreach (int x in nums)
17+
{
18+
// Decrement the frequency of x in 'right_map' since x is now being
19+
// processed and is no longer to the right.
20+
rightMap[x] = rightMap.GetValueOrDefault(x) - 1;
21+
if (x % r == 0)
22+
{
23+
count += leftMap.GetValueOrDefault(x / r) * rightMap.GetValueOrDefault(x * r);
24+
}
25+
// Increment the frequency of x in 'left_map' since it'll be a part of the
26+
// left side of the array once we iterate to the next value of x.
27+
leftMap[x] = leftMap.GetValueOrDefault(x) + 1;
28+
}
29+
30+
return count;
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution
2+
{
3+
public int LongestChainOfConsecutiveNumbers(int[] nums)
4+
{
5+
if (nums == null || nums.Length == 0)
6+
{
7+
return 0;
8+
}
9+
HashSet<int> numSet = new HashSet<int>(nums);
10+
int longestChain = 0;
11+
foreach (int num in numSet)
12+
{
13+
// If the current number is the smallest number in its chain, search for
14+
// the length of its chain.
15+
if (!numSet.Contains(num - 1))
16+
{
17+
int currentNum = num;
18+
int currentChain = 1;
19+
// Continue to find the next consecutive numbers in the chain.
20+
while (numSet.Contains(currentNum + 1))
21+
{
22+
currentNum++;
23+
currentChain++;
24+
}
25+
longestChain = Math.Max(longestChain, currentChain);
26+
}
27+
}
28+
29+
return longestChain;
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution
2+
{
3+
public int LongestChainOfConsecutiveNumbersBruteForce(int[] nums)
4+
{
5+
if (nums == null || nums.Length == 0)
6+
{
7+
return 0;
8+
}
9+
int longestChain = 0;
10+
// Look for chains of consecutive numbers that start from each number.
11+
foreach (int num in nums)
12+
{
13+
int currentNum = num;
14+
int currentChain = 1;
15+
// Continue to find the next consecutive numbers in the chain.
16+
while (nums.Contains(currentNum + 1))
17+
{
18+
currentNum++;
19+
currentChain++;
20+
}
21+
longestChain = Math.Max(longestChain, currentChain);
22+
}
23+
24+
return longestChain;
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution
2+
{
3+
public int[] PairSumUnsorted(int[] nums, int target)
4+
{
5+
Dictionary<int, int> hashmap = new Dictionary<int, int>();
6+
for (int i = 0; i < nums.Length; i++)
7+
{
8+
if (hashmap.ContainsKey(target - nums[i]))
9+
{
10+
return [hashmap[target - nums[i]], i];
11+
}
12+
13+
hashmap[nums[i]] = i;
14+
}
15+
16+
return [];
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution
2+
{
3+
public int[] PairSumUnsortedTwoPass(int[] nums, int target)
4+
{
5+
Dictionary<int, int> numMap = new Dictionary<int, int>();
6+
// First pass: Populate the hash map with each number and its
7+
// index
8+
for (int i = 0; i < nums.Length; i++)
9+
{
10+
numMap[nums[i]] = i;
11+
}
12+
// Second pass: Check for each number's complement in the hash map.
13+
for (int i = 0; i < nums.Length; i++)
14+
{
15+
int complement = target - nums[i];
16+
if (numMap.ContainsKey(complement) && numMap[complement] != i)
17+
{
18+
return [i, numMap[complement]];
19+
}
20+
}
21+
22+
return [];
23+
}
24+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
public class Solution
2+
{
3+
public bool VerifySudokuBoard(int[][] board)
4+
{
5+
// Create hash sets for each row, column, and subgrid to keep
6+
// track of numbers previously seen on any given row, column, or
7+
// subgrid.
8+
HashSet<int>[] rowSets = new HashSet<int>[9];
9+
HashSet<int>[] columnSets = new HashSet<int>[9];
10+
HashSet<int>[,] subgridSets = new HashSet<int>[3, 3];
11+
12+
// Initializing row and column sets.
13+
for (int i = 0; i < 9; i++)
14+
{
15+
rowSets[i] = new HashSet<int>();
16+
columnSets[i] = new HashSet<int>();
17+
}
18+
19+
// Initializing the subgrid sets.
20+
for (int i = 0; i < 3; i++)
21+
{
22+
for (int j = 0; j < 3; j++)
23+
{
24+
subgridSets[i, j] = new HashSet<int>();
25+
}
26+
}
27+
28+
for (int r = 0; r < 9; r++)
29+
{
30+
for (int c = 0; c < 9; c++)
31+
{
32+
int num = board[r][c];
33+
if (num == 0)
34+
{
35+
continue;
36+
}
37+
// Check if 'num' has been seen in the current row,
38+
// column, or subgrid.
39+
if (rowSets[r].Contains(num))
40+
{
41+
return false;
42+
}
43+
if (columnSets[c].Contains(num))
44+
{
45+
return false;
46+
}
47+
if (subgridSets[r / 3, c / 3].Contains(num))
48+
{
49+
return false;
50+
}
51+
// If we passed the above checks, mark this value as seen
52+
// by adding it to its corresponding hash sets.
53+
rowSets[r].Add(num);
54+
columnSets[c].Add(num);
55+
subgridSets[r / 3, c / 3].Add(num);
56+
}
57+
}
58+
59+
return true;
60+
}
61+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
public class Solution
2+
{
3+
public void ZeroStriping(int[][] matrix)
4+
{
5+
if (matrix == null || matrix.Length == 0 || matrix[0] == null || matrix[0].Length == 0)
6+
{
7+
return;
8+
}
9+
int m = matrix.Length, n = matrix[0].Length;
10+
// Check if the first row initially contains a zero.
11+
bool firstRowHasZero = false;
12+
for (int c = 0; c < n; c++)
13+
{
14+
if (matrix[0][c] == 0)
15+
{
16+
firstRowHasZero = true;
17+
break;
18+
}
19+
}
20+
// Check if the first column initially contains a zero.
21+
bool firstColHasZero = false;
22+
for (int r = 0; r < m; r++)
23+
{
24+
if (matrix[r][0] == 0)
25+
{
26+
firstColHasZero = true;
27+
break;
28+
}
29+
}
30+
// Use the first row and column as markers. If an element in the
31+
// submatrix is zero, mark its corresponding row and column in the
32+
// first row and column as 0.
33+
for (int r = 1; r < m; r++)
34+
{
35+
for (int c = 1; c < n; c++)
36+
{
37+
if (matrix[r][c] == 0)
38+
{
39+
matrix[0][c] = 0;
40+
matrix[r][0] = 0;
41+
}
42+
}
43+
}
44+
// Update the submatrix using the markers in the first row and
45+
// column.
46+
for (int r = 1; r < m; r++)
47+
{
48+
for (int c = 1; c < n; c++)
49+
{
50+
if (matrix[0][c] == 0 || matrix[r][0] == 0)
51+
{
52+
matrix[r][c] = 0;
53+
}
54+
}
55+
}
56+
// If the first row had a zero initially, set all elements in the
57+
// first row to zero.
58+
if (firstRowHasZero)
59+
{
60+
for (int c = 0; c < n; c++)
61+
{
62+
matrix[0][c] = 0;
63+
}
64+
}
65+
// If the first column had a zero initially, set all elements in
66+
// the first column to zero.
67+
if (firstColHasZero)
68+
{
69+
for (int r = 0; r < m; r++)
70+
{
71+
matrix[r][0] = 0;
72+
}
73+
}
74+
}
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Solution
2+
{
3+
public void ZeroStripingHashSets(int[][] matrix)
4+
{
5+
if (matrix == null || matrix.Length == 0 || matrix[0] == null || matrix[0].Length == 0)
6+
{
7+
return;
8+
}
9+
int m = matrix.Length, n = matrix[0].Length;
10+
HashSet<int> zeroRows = new HashSet<int>();
11+
HashSet<int> zeroCols = new HashSet<int>();
12+
// Pass 1: Traverse through the matrix to identify the rows and
13+
// columns containing zeros and store their indexes in the
14+
// appropriate hash sets.
15+
for (int r = 0; r < m; r++)
16+
{
17+
for (int c = 0; c < n; c++)
18+
{
19+
if (matrix[r][c] == 0)
20+
{
21+
zeroRows.Add(r);
22+
zeroCols.Add(c);
23+
}
24+
}
25+
}
26+
// Pass 2: Set any cell in the matrix to zero if its row index is
27+
// in 'zero_rows' or its column index is in 'zero_cols'.
28+
for (int r = 0; r < m; r++)
29+
{
30+
for (int c = 0; c < n; c++)
31+
{
32+
if (zeroRows.Contains(r) || zeroCols.Contains(c))
33+
{
34+
matrix[r][c] = 0;
35+
}
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)