2.08 ✅ Quay lui (Backtracking)

Các dạng bài toán:

  • Hoán vị (Permutations): Bài 46, 47, 60, 526, 996
  • Tổ hợp (Combination): Bài 39, 40, 77, 216
  • Bài toán lai giữa hoán vị và tổ hợp: Bài 1079
  • N-Queens – lời giải tối ưu (dùng bit): Bài 51, 52
  • Sudoku: Bài 37
  • Tìm kiếm 4 hướng: Bài 79, 212, 980
  • Tập con (Subsets): Bài 78, 90
  • Trie: Bài 208, 211
  • Tối ưu BFS: Bài 126, 127
  • Mẫu DFS: (chỉ là ví dụ, không gắn với bài cụ thể nào)
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] { // Đây là logic then chốt để loại bỏ trùng lặp continue } if target >= nums[i] { c = append(c, nums[i]) findcombinationSum2(nums, target-nums[i], i+1, c, res) c = c[:len(c)-1] } } }
  • Mẫu BFS: (chỉ là ví dụ minh họa, không tương ứng với bài cụ thể nào)
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 }
STTTiêu đềLời giảiĐộ khóĐộ phức tạp thời gianĐộ phức tạp bộ nhớYêu thíchTỷ lệ AC (Acceptance)
0017Tổ hợp chữ cái của số điện thoại (Letter Combinations of a Phone Number)GoMediumO(log n)O(1)56.6%
0022Sinh dấu ngoặc (Generate Parentheses)GoMediumO(log n)O(1)72.5%
0037Giải Sudoku (Sudoku Solver)GoHardO(n^2)O(n^2)❤️57.7%
0039Tổng tổ hợp (Combination Sum)GoMediumO(n log n)O(n)68.6%
0040Tổng tổ hợp II (Combination Sum II)GoMediumO(n log n)O(n)53.4%
0046Hoán vị (Permutations)GoMediumO(n)O(n)❤️75.7%
0047Hoán vị II (Permutations II)GoMediumO(n^2)O(n)❤️57.4%
0051Bài toán N quân hậu (N-Queens)GoHardO(n!)O(n)❤️64.2%
0052Bài toán N quân hậu II (N-Queens II)GoHardO(n!)O(n)❤️71.6%
0077Tổ hợp (Combinations)GoMediumO(n)O(n)❤️67.0%
0078Các tập con (Subsets)GoMediumO(n^2)O(n)❤️74.9%
0079Tìm từ (Word Search)GoMediumO(n^2)O(n^2)❤️40.2%
0089Mã Gray (Gray Code)GoMediumO(n)O(1)57.2%
0090Các tập con II (Subsets II)GoMediumO(n^2)O(n)❤️55.9%
0093Khôi phục địa chỉ IP (Restore IP Addresses)GoMediumO(n)O(n)❤️47.4%
0095Cây nhị phân tìm kiếm duy nhất II (Unique Binary Search Trees II)GoMedium52.4%
0113Tổng đường đi II (Path Sum II)GoMedium57.1%
0126Thang từ II (Word Ladder II)GoHardO(n)O(n^2)❤️27.5%
0131Phân hoạch chuỗi đối xứng (Palindrome Partitioning)GoMediumO(n)O(n^2)❤️64.9%
0212Tìm từ II (Word Search II)GoHardO(n^2)O(n^2)❤️36.4%
0216Tổng tổ hợp III (Combination Sum III)GoMediumO(n)O(1)❤️67.6%
0257Các đường đi của cây nhị phân (Binary Tree Paths)GoEasy61.4%
0301Loại bỏ dấu ngoặc không hợp lệ (Remove Invalid Parentheses)GoHard47.2%
0306Số cộng dồn (Additive Number)GoMediumO(n^2)O(1)❤️31.1%
0357Đếm số có chữ số duy nhất (Count Numbers with Unique Digits)GoMediumO(1)O(1)51.9%
0401Đồng hồ nhị phân (Binary Watch)GoEasyO(1)O(1)52.3%
0473Diêm que tạo hình vuông (Matchsticks to Square)GoMedium40.2%
0491Các dãy con không giảm (Non-decreasing Subsequences)GoMedium60.2%
0494Tổng mục tiêu (Target Sum)GoMedium45.7%
0526Sắp xếp đẹp (Beautiful Arrangement)GoMediumO(n^2)O(1)❤️64.4%
0638Ưu đãi mua sắm (Shopping Offers)GoMedium53.3%
0784Hoán vị chữ hoa/thường (Letter Case Permutation)GoMediumO(n)O(n)73.8%
0816Tọa độ mơ hồ (Ambiguous Coordinates)GoMedium56.4%
0842Chia mảng thành dãy Fibonacci (Split Array into Fibonacci Sequence)GoMediumO(n^2)O(1)❤️38.4%
0980Số đường đi duy nhất III (Unique Paths III)GoHardO(n log n)O(n)81.7%
0996Số lượng mảng squareful (Number of Squareful Arrays)GoHardO(n log n)O(n)49.2%
1079Số khả năng sắp xếp gạch chữ (Letter Tile Possibilities)GoMediumO(n^2)O(1)❤️76.0%
1239Độ dài tối đa của chuỗi ghép có ký tự duy nhất (Maximum Length of a Concatenated String with Unique Characters)GoMedium52.2%
1655Phân phối số nguyên lặp lại (Distribute Repeating Integers)GoHard39.3%
------------------------------------------------------------------------------------------------------------------------------------------------