Greedy Problems - I

Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. So the problems where choosing locally optimal also leads to global solution are best fit for Greedy.

Read More

Backtracking - Chessboard

Backtracking can also be used to solve chessboard problems.

51. N-Queens

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
func solveNQueens(n int) [][]string {
result := [][]string{}
board := make([][]string, n)
for i := 0; i < n; i++ {
board[i] = make([]string, n)
for j := 0; j < n; j++ {
board[i][j] = "."
}
}
isValid := func(row, col int) bool {
for i := 0; i < row; i++ {
if board[i][col] == "Q" {
return false
}
}
for i := 0; i < col; i++ {
if board[row][i] == "Q" {
return false
}
}
for i, j := row, col; i >= 0 && j >= 0; i, j = i - 1, j - 1 {
if board[i][j] == "Q" {
return false
}
}
for i, j := row, col; i >= 0 && j < n; i, j = i - 1, j + 1 {
if board[i][j] == "Q" {
return false
}
}
return true
}
var backtracking func(int)
backtracking = func(row int) {
if row == n {
temp := make([]string, n)
for i, boardRow := range board {
temp[i] = strings.Join(boardRow, "")
}
result = append(result, temp)
return
}
for col := 0; col < n; col++ {
if isValid(row, col) {
board[row][col] = "Q"
backtracking(row + 1)
board[row][col] = "."
}
}
}
backtracking(0)
return result
}

Read More

Backtracking - Subsets

Backtracking can also help us to get all subsets of a given slice. If Combination and Partitioning problems can be converted to get root-to-leaf paths during a tree DFS traversal, Subsets can be treated as getting all root-to-node paths during a tree DFS traversal.

Read More

Backtracking - Partioning

Partitioning is another classical problem which can be solved with backtracking algorithm.

131. Palindrome Partitioning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
func partition(s string) [][]string {
result := [][]string{}
path := []string{}
length := len(s)
isParlindrom := func(s string) bool {
for i, j := 0, len(s) - 1; i < j; i, j := i + 1, j - 1 {
if s[i] != s[j] {
return false
}
}
return true
}
var backtracking func(int)
backtracking = func(index int) {
if index == length {
temp := make([]int, len(path))
copy(temp, path)
result = append(result, temp)
return
}
for i := index; i < length; i++ {
if isParlindrom(s[index:i + 1]) {
path = append(path, s[index: i + 1])
backtracking(i + 1)
path = path[:len(path) - 1]
}
}
}
backtracking(0)
return result
}

Read More

Backtracking - Combinations

Backtracking is an algorithmic-technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time (by time, here, is referred to the time elapsed till reaching any level of the search tree). Usually we can consider backtracking as DFS recursively traversal.

Read More

Modify Trees

108. Convert Sorted Array to Binary Search Tree

Highed balanced means left nodes and right nodes have the minimized same size difference.

Read More

Search in Trees

700. Search in a Binary Search Tree

Naive BST query.

Read More

iBeacon distance measurement

Bluetooth technology has been widely used in various devices, such as earphones, speakers, smart watches and so on. One of the important applications is Bluetooth positioning technology, which can determine the distance between devices by measuring the strength of Bluetooth signals. This method of distance estimation based on signal strength is called RSSI (Received Signal Strength Indication) ranging. In this article, we will discuss the principle, implementation, and related application scenarios of RSSI ranging.

Read More

Construct and Update a Tree

1008. Construct Binary Search Tree from Preorder Traversal

Based on the preorder traversal definition for a BST, the first element in the slice is always coming from the root node, we can split the rest elements into two parts from the element which is no less than the root node for child nodes.

Read More

Tree Properties

101. Symmetric Tree

a. DFS solution

Read More