Algorithm in LeetCode —— Backtracking

Tips for Backtracking:

  • Permutation problems. Problem 46, Problem 47. Problem 60, Problem 526, Problem 996.
  • Combination problems. Problem 39, Problem 40, Problem 77, Problem 216.
  • Hybrid permutation and combination problems. Problem 1079.
  • Ultimate solution for N-Queens (binary solution). Problem 51, Problem 52.
  • Sudoku problems. Problem 37.
  • Four-directional search. Problem 79, Problem 212, Problem 980.
  • Subset problems. Problem 78, Problem 90.
  • Trie. Problem 208, Problem 211.
  • BFS optimization. Problem 126, Problem 127.
  • DFS template. (Just an example; not tied to any specific problem)
func combinationSum2(candidates []int, target int) [][]int {
	if len(candidates) == 0 {
		return [][]int{}
	}
	c, res := []int{}, [][]int{}
	sort.Ints(candidates)
	findcombinationSum2(candidates, target, 0, c, &res)
	return res
}

func findcombinationSum2(nums []int, target, index int, c []int, res *[][]int) {
	if target == 0 {
		b := make([]int, len(c))
		copy(b, c)
		*res = append(*res, b)
		return
	}
	for i := index; i < len(nums); i++ {
		if i > index && nums[i] == nums[i-1] { // This is the key logic for deduplication
			continue
		}
		if target >= nums[i] {
			c = append(c, nums[i])
			findcombinationSum2(nums, target-nums[i], i+1, c, res)
			c = c[:len(c)-1]
		}
	}
}
  • BFS template. (Just an example; it does not correspond to any specific problem)
func updateMatrix_BFS(matrix [][]int) [][]int {
	res := make([][]int, len(matrix))
	if len(matrix) == 0 || len(matrix[0]) == 0 {
		return res
	}
	queue := make([][]int, 0)
	for i, _ := range matrix {
		res[i] = make([]int, len(matrix[0]))
		for j, _ := range res[i] {
			if matrix[i][j] == 0 {
				res[i][j] = -1
				queue = append(queue, []int{i, j})
			}
		}
	}
	level := 1
	for len(queue) > 0 {
		size := len(queue)
		for size > 0 {
			size -= 1
			node := queue[0]
			queue = queue[1:]
			i, j := node[0], node[1]
			for _, direction := range [][]int{{-1, 0}, {1, 0}, {0, 1}, {0, -1}} {
				x := i + direction[0]
				y := j + direction[1]
				if x < 0 || x >= len(matrix) || y < 0 || y >= len(matrix[0]) || res[x][y] < 0 || res[x][y] > 0 {
					continue
				}
				res[x][y] = level
				queue = append(queue, []int{x, y})
			}
		}
		level++
	}
	for i, row := range res {
		for j, cell := range row {
			if cell == -1 {
				res[i][j] = 0
			}
		}
	}
	return res
}
TitleSolutionDifficultyTimeSpaceFavorites
17. Letter Combinations of a Phone NumberGoMediumO(log n)O(1)
22. Generate ParenthesesGoMediumO(log n)O(1)
37. Sudoku SolverGoHardO(n^2)O(n^2)❤️
39. Combination SumGoMediumO(n log n)O(n)
40. Combination Sum IIGoMediumO(n log n)O(n)
46. PermutationsGoMediumO(n)O(n)❤️
47. Permutations IIGoMediumO(n^2)O(n)❤️
51. N-QueensGoHardO(n^2)O(n)❤️
52. N-Queens IIGoHardO(n^2)O(n)❤️
60. Permutation SequenceGoMediumO(n log n)O(1)
77. CombinationsGoMediumO(n)O(n)❤️
78. SubsetsGoMediumO(n^2)O(n)❤️
79. Word SearchGoMediumO(n^2)O(n^2)❤️
89. Gray CodesGoMediumO(n)O(1)
90. Subsets IIGoMediumO(n^2)O(n)❤️
93. Restore IP AddressesGoMediumO(n)O(n)❤️
126. Word Ladder IIGoHardO(n)O(n^2)❤️
131. Palindrome PartitioningGoMediumO(n)O(n^2)❤️
211. Add and Search Word - Data structure designGoMediumO(n)O(n)❤️
212. Word Search IIGoHardO(n^2)O(n^2)❤️
216. Combination Sum IIIGoMediumO(n)O(1)❤️
306. Additive NumberGoMediumO(n^2)O(1)❤️
357. Count Numbers with Unique DigitsGoMediumO(1)O(1)
401. Binary WatchGoEasyO(1)O(1)
526. Beautiful ArrangementGoMediumO(n^2)O(1)❤️
784. Letter Case PermutationGoEasyO(n)O(n)
842. Split Array into Fibonacci SequenceGoMediumO(n^2)O(1)❤️
980. Unique Paths IIIGoHardO(n log n)O(n)
996. Number of Squareful ArraysGoHardO(n log n)O(n)
1079. Letter Tile PossibilitiesGoMediumO(n^2)O(1)❤️