Skip to content

Commit aa3a789

Browse files
committed
Go: Chapter 2 Hash Maps and Sets
1 parent 5ef5c80 commit aa3a789

8 files changed

+232
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package hashmapsandsets
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package hashmapsandsets
2+
3+
func LongestChainOfConsecutiveNumbers(nums []int) int {
4+
if len(nums) == 0 {
5+
return 0
6+
}
7+
numSet := make(map[int]bool)
8+
for _, num := range nums {
9+
numSet[num] = true
10+
}
11+
longestChain := 0
12+
for num := range numSet {
13+
// If the current number is the smallest number in its chain, search for
14+
// the length of its chain.
15+
if !numSet[num-1] {
16+
currentNum := num
17+
currentChain := 1
18+
// Continue to find the next consecutive numbers in the chain.
19+
for numSet[currentNum+1] {
20+
currentNum++
21+
currentChain++
22+
}
23+
if currentChain > longestChain {
24+
longestChain = currentChain
25+
}
26+
}
27+
}
28+
return longestChain
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package hashmapsandsets
2+
3+
func LongestChainOfConsecutiveNumbersBruteForce(nums []int) int {
4+
if len(nums) == 0 {
5+
return 0
6+
}
7+
longestChain := 0
8+
// Look for chains of consecutive numbers that start from each number.
9+
for _, num := range nums {
10+
currentNum := num
11+
currentChain := 1
12+
// Continue to find the next consecutive numbers in the chain.
13+
for contains(nums, currentNum+1) {
14+
currentNum++
15+
currentChain++
16+
}
17+
if currentChain > longestChain {
18+
longestChain = currentChain
19+
}
20+
}
21+
return longestChain
22+
}
23+
24+
// In the Python code, the while loop checks (current_num +1) in nums.
25+
// So the helper function 'contains' is needed.
26+
func contains(nums []int, target int) bool {
27+
for _, n := range nums {
28+
if n == target {
29+
return true
30+
}
31+
}
32+
return false
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hashmapsandsets
2+
3+
func PairSumUnsorted(nums []int, target int) []int {
4+
hashmap := make(map[int]int)
5+
for i, x := range nums {
6+
complement := target - x
7+
if idx, exists := hashmap[complement]; exists {
8+
return []int{idx, i}
9+
}
10+
hashmap[x] = i
11+
}
12+
return nil
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package hashmapsandsets
2+
3+
func PairSumUnsortedTwoPass(nums []int, target int) []int {
4+
numMap := make(map[int]int)
5+
// First pass: Populate the hash map with each number and its index.
6+
for i, num := range nums {
7+
numMap[num] = i
8+
}
9+
// Second pass: Check for each number's complement in the hash map.
10+
for i, num := range nums {
11+
complement := target - num
12+
if idx, ok := numMap[complement]; ok && idx != i {
13+
return []int{i, idx}
14+
}
15+
}
16+
return nil
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package hashmapsandsets
2+
3+
func VerifySudokuBoard(board [][]int) bool {
4+
// Create hash sets for each row, column, and subgrid to keep
5+
// track of numbers previously seen on any given row, column, or
6+
// subgrid.
7+
rowSets := make([]map[int]struct{}, 9)
8+
columnSets := make([]map[int]struct{}, 9)
9+
subgridSets := make([][]map[int]struct{}, 3)
10+
11+
for i := range rowSets {
12+
rowSets[i] = make(map[int]struct{})
13+
}
14+
for i := range columnSets {
15+
columnSets[i] = make(map[int]struct{})
16+
}
17+
for i := range subgridSets {
18+
subgridSets[i] = make([]map[int]struct{}, 3)
19+
for j := range subgridSets[i] {
20+
subgridSets[i][j] = make(map[int]struct{})
21+
}
22+
}
23+
24+
for r := 0; r < 9; r++ {
25+
for c := 0; c < 9; c++ {
26+
num := board[r][c]
27+
if num == 0 {
28+
continue
29+
}
30+
// Check if 'num' has been seen in the current row,
31+
// column, or subgrid.
32+
if _, exists := rowSets[r][num]; exists {
33+
return false
34+
}
35+
if _, exists := columnSets[c][num]; exists {
36+
return false
37+
}
38+
if _, exists := subgridSets[r/3][c/3][num]; exists {
39+
return false
40+
}
41+
// If we passed the above checks, mark this value as seen
42+
// by adding it to its corresponding hash sets.
43+
rowSets[r][num] = struct{}{}
44+
columnSets[c][num] = struct{}{}
45+
subgridSets[r/3][c/3][num] = struct{}{}
46+
}
47+
}
48+
return true
49+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package hashmapsandsets
2+
3+
func ZeroStriping(matrix [][]int) {
4+
if len(matrix) == 0 || len(matrix[0]) == 0 {
5+
return
6+
}
7+
m, n := len(matrix), len(matrix[0])
8+
// Check if the first row initially contains a zero.
9+
firstRowHasZero := false
10+
for c := 0; c < n; c++ {
11+
if matrix[0][c] == 0 {
12+
firstRowHasZero = true
13+
break
14+
}
15+
}
16+
// Check if the first column initially contains a zero.
17+
firstColHasZero := false
18+
for r := 0; r < m; r++ {
19+
if matrix[r][0] == 0 {
20+
firstColHasZero = true
21+
break
22+
}
23+
}
24+
// Use the first row and column as markers. If an element in the
25+
// submatrix is zero, mark its corresponding row and column in the
26+
// first row and column as 0.
27+
for r := 1; r < m; r++ {
28+
for c := 1; c < n; c++ {
29+
if matrix[r][c] == 0 {
30+
matrix[0][c] = 0
31+
matrix[r][0] = 0
32+
}
33+
}
34+
}
35+
// Update the submatrix using the markers in the first row and
36+
// column.
37+
for r := 1; r < m; r++ {
38+
for c := 1; c < n; c++ {
39+
if matrix[0][c] == 0 || matrix[r][0] == 0 {
40+
matrix[r][c] = 0
41+
}
42+
}
43+
}
44+
// If the first row had a zero initially, set all elements in the
45+
// first row to zero.
46+
if firstRowHasZero {
47+
for c := 0; c < n; c++ {
48+
matrix[0][c] = 0
49+
}
50+
}
51+
// If the first column had a zero initially, set all elements in
52+
// the first column to zero.
53+
if firstColHasZero {
54+
for r := 0; r < m; r++ {
55+
matrix[r][0] = 0
56+
}
57+
}
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package hashmapsandsets
2+
3+
func ZeroStripingHashSets(matrix [][]int) {
4+
if len(matrix) == 0 || len(matrix[0]) == 0 {
5+
return
6+
}
7+
m, n := len(matrix), len(matrix[0])
8+
zeroRows := make(map[int]struct{})
9+
zeroCols := make(map[int]struct{})
10+
// Pass 1: Traverse through the matrix to identify the rows and
11+
// columns containing zeros and store their indexes in the
12+
// appropriate hash sets.
13+
for r := 0; r < m; r++ {
14+
for c := 0; c < n; c++ {
15+
if matrix[r][c] == 0 {
16+
zeroRows[r] = struct{}{}
17+
zeroCols[c] = struct{}{}
18+
}
19+
}
20+
}
21+
// Pass 2: Set any cell in the matrix to zero if its row index is
22+
// in 'zero_rows' or its column index is in 'zero_cols'.
23+
for r := 0; r < m; r++ {
24+
for c := 0; c < n; c++ {
25+
if _, ok := zeroRows[r]; ok {
26+
matrix[r][c] = 0
27+
} else if _, ok := zeroCols[c]; ok {
28+
matrix[r][c] = 0
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)