Leetcode backtracking Top interview Question 7 questions summary

This article is a summary of the next leetcode tree and Top Interview Questions label 7 backtracking types of topics

Leetcode 46. Permuation
seeking an array arrangement, constantly adding new elements recursive, recursive after deleting, and adding a new element in the same position in a new cycle

Leetcode 22. Generate Parentheses
arranged demand (and), the above ideas, just to satisfy the correspondence between the left and right parentheses

Leetcode 78. subsets
a subset of a set of demand
idea a: the same arrangement, constantly adding new recursive element, deleting the new element after the
idea two: each iteration, the previous iteration followed by the results of each current iteration number of course, before the addition of each iteration to retain a result (this idea is similar to a subset of a code workBreakII also find a string can be divided out)

Leetcode 17. Letter Combinations of a Phone Number
with Permutation, can be seen as just Permutation A traverse from one array to the same recursive array A, and this problem is traversed from the array to the array of digits chs

Leetcode 131. Palindrome Partitioning
idea a: insertion problem can be abstracted as a separator, can be seen from left to right, is inserted continuously into a separator s, so that s into left and right portions, left when the palindrome, and then right back to the recursive, insert a separator (backtracking recursive procedure can be seen as divided from left to right, the left is set in line with the minimum requirements, does not require recursion, then recursively to the right side)
thinking two: a palindromic sequence may be dynamic programming judge

Leetcode 140. Word Break II
idea a: insert separators can be seen as a problem, but may result in backtracking normal TLE
idea II: introducing DP, records corresponding to all the sentences in the string s

leetcode 79. Word Search
normal in four directions traverse back

summary

  1. 回溯的时间复杂度的判断:
    The way I like to think about the runtime of backtracking algorithms is O(b^d), where b is the branching factor and d is the maximum depth of recursion.
    Backtracking is characterized by a number of decisions b that can be made at each level of recursion.If you visualize the recursion tree, this is the number of children each internal node has.You can also think of b as standing for “base”, which can help you remember that b is the base of the exponential.
    If we can make b decisions at each level of recursion, and we expand the recursion tree to d levels(ie: each path has a length of d), then we get b^d nodes.Since backtracking is exhaustive and must visit each one of these nodes, the runtime is O(b^d).
  2. Many are based on a template Permutation back in for recursive loop, adding new elements, after the completion of the recursively, deleting the element. Adding new elements in the same position in the new cycle
  3. Backtracking parameter template:
    A to join a start index, indicating the new recursive elements which should be from the beginning.
    B not start using the index, each recursive directly into the new array / string, for each cycle is from 0. begin traversal (also suggested the template, which will help to think more)
  4. Backtracking usually times, thus optimizing binding dp, can be drawn to answer the tree, find the repeated calculation portion, thereby optimizing the portion
  5. There is a class template is divided on the back of a string, can be seen from left to right to insert a separator string, the string will be divided into two parts left and right, and then make the left is its minimal set, not recursive, then recursive backtracking right part
  6. Seeking a subset of the array (no matter what form a subset), are thinking this way: using an iterative rather than recursive, each iteration, every previous iteration of the results followed by a number of the current iteration, of course, plus to retain the results of each iteration before
Published 91 original articles · won praise 9 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_38702697/article/details/104308795