Leetcode Classification Analysis: Combinatorial algorithm

Source: Internet
Author: User

Leetcode Classification Analysis: Combinatorial algorithm

The so-called combinatorial algorithm refers to: in solving some algorithmic problems, it is necessary to produce various combinations of input data, permutations, subsets, partitions and so on, and then one by one to confirm that each is not the solution we want. Broadly speaking, the combinatorial algorithm can be all-encompassing, even sorting, a variety of search algorithms can be counted in. Recently read "The algorithm Design Manual" learned this classification, the Internet a check, even a special book to explain, and Knuth's magnum opus TAOCP fourth volume is called Combinatorial algorithm, it seems really ignorant! So recently focused on the leetcode of all the relevant topics, here to collate the learning experience. Problem quantity is important, quality is also important!

1. Classification Map

Personally think that the combination algorithm for a large class is a very good classification, than the current online to see some similar to the exhaustive, BFS, Dfs classification method to be much clearer. Let's take a look at the location of the combinatorial algorithm in this series, and it can be subdivided into small pieces:

    1. infrastructure (Fundamentals)
      1.1 Arrays and linked lists (array&list): Insert, delete, rotate, and so on.
      1.2 Stacks and Queues (stack&queue): Typical applications for stacks.
      1.3 Tree: Build, validate, traverse, transform.
      1.4 String: Transform, search, operation.
    2. Building Blocks (Building block)
      2.1 Hash Table (Hashing)
      2.2 Division (Divide-and-conquer)
      2.3 Sort (sorting)
      2.4 Binary search (binary search)
    3. Advanced Algorithm:
      3.1 Combination algorithm (combinatorial algorithm):
      -Backtracking (backtracking)
      -Combination (combination)
      -Subset (subset)
      -Arrangement (permutation)
      -Partition (Partition)
      3.2 Greedy algorithm (greedy algorithm): A typical application of greed.
      3.3 Dynamic Programming: DP is widely used to find the optimal solution.
    4. other miscellaneous (MISC):
      4.1 Mathematics (Math)
      4.2-bit operation (bit manipulation)
      4.3 Matrices (matrix)
2. Strategies for Solving problems

On the problem solving strategy of combinatorial algorithm, the 7th and 14th chapters of Red Book "The algorithm Design Manual" are described in detail. If not enough, you can refer to Knuth's macro book "The Art of Computer Programming" 4a volume. Backtracking is a typical technique for enumerating all possible solutions to implement combinatorial algorithms. "The algorithm Design Manual" In addition to the basic problems, but also introduced some clever pruning techniques, not involved in this, or to Leetcode as the blueprint, to avoid irrelevant.

2.1 Recursive invocation, deep search, and backtracking techniques

Beginners, inevitably on the backtracking, DFS, recursion, the relationship between the three are not clear, feel as if it is a thing, it is not. To clarify the relationship between the three, first recursion, then DFS/BFS, and finally look at the retrospective glance.

2.1.1 Recursion (recursion)

Recursion can be used to implement various algorithms that conform to the recursive structure (writing a recursive article, "The cornerstone of program design: Recursion"). From the implementation mechanism, recursion is just a program written by the programming language, which is implemented by stack frame when the operating system runs the program . From the way of solving the problem, it is similar to the way of solving the problem in the past, the recursion is bottom-up, the solution of the smaller problem is obtained, and then the solution of the big problem is gradually merged. Taking Leetcode exercises as an example, the most typical is the recursive implementation of the Divide-and-conquer strategy, can solve a large class of problems, so it is not limited to DFS and backtracking issues.

2.1.2 Depth/Breadth First search (DFS/BFS)

The so-called depth-priority DFS, breadth-first BFS, is the term of the graph (tree) category, specifically, the way to step through the nodes in the graph. In general, DFS is easier to implement recursively because we take full advantage of the convenience provided by the programming language to give the OS the maintenance of the stack. Of course, we can ignore this kind of traversal, our own display uses the Loop +stack way realizes. BFS is generally used in a circular +queue way to achieve, because there is no such as recursive simple implementation mechanism, so slightly more trouble. taking Leetcode exercises as an example, the pre-and post-order traversal of a tree belongs to DFS, while the level sequence traversal (and various variant problems like Zigzag) belong to BFS.

2.1.3 Backtracking (backtracking)

Finally speaking of backtracking, "the algorithm Design Manual" gives the definition: "backtracking can be viewed as a depth-first search in an implicit graph", with a tree (figure) To represent the execution of recursion (which is a natural way of studying recursion), backtracking is the way in which a DFS search is performed on an implicit graph to construct a solution . The so-called implicit diagram at the beginning of the 5th chapter has an explanation: it means that we will not be the entire recursive process of backtracking to form a tree (figure) completely generated, but with the implementation of backtracking, a little bit of construction, a bit like the game map. In fact it is easy to understand, because a lot of problems are not to find all the solution, but to determine that there is a solution to end the backtracking, so there is no need to go through the entire search space. Of course, the definition above is not absolute, why not use BFS? The algorithm Design Manual explains that because the execution process tree is not too high for most problems, the width of the tree increases exponentially as the tree moves down one layer at a time , so we typically choose DFS for backtracking. and thanks to the entry and exit functions at each recursion, we can easily manage the backtracking state . Take Leetcode exercise as an example, the exception must be used in BFS to achieve otherwise the problem is: 127-word ladder and 130-surrounded regions. The former is because of the nature of the problem itself, each downward layer while narrowing the search space, so BFS will not cause too much problem. And the latter is no reason, without BFS can not live online judge. So just practice it as a typical problem of backtracking from Dfs back to BFS.

Typical questions are: 79-word search and 130-surrounded regions.

2.2 Typical sub-problems

In the algorithm Design Manual, a template-style code can be seen as a standard template for solving combinatorial problems. There are a few key steps that can be tailored to different issues: 1) Determine if it is a solution: for example, you have accessed the deepest, or reached the target condition. At this point, depending on the requirements, you may return directly, you may want to save the path. 2) construct candidate values: Confirm the range of candidate values before starting a new round of recursive calls. Here is also the key to pruning! 3) Recursive invocation: You can display the declaration parameter K for recursive depth, or you can use the size of the path or the target value decrement to determine when the return is implicit.

void backtrack(result, path, k, input) {    if// k==input.length, target==0...        result.add(path);        return//returnreturntruereturn1...    }    candidates = construct();    for: candidates) {        backtrack(result, path, k+1, input);    }}

The following sub-problems are basically based on this routine. It is a template to solve the problem of composition, in fact, if the recursive understanding is deep, there is no need to memorize what template code. The combinatorial algorithm is highly dependent on the powerful recursion, so its strategy of solving the problem is simply the specificity of recursion technology. Based on the bottom-up recursive general problem-solving method, it is natural to design the correct program. About the general solution, keep the special talk about recursion when you say it.

2.2.1 Combinations (combination) and subsets (subset)

A combination is really similar to a subset problem (subset) and should be counted as a subset problem. So in "the algorithm Design Manual" There is no separate section, but a subset. The specific solution is: for the combination, use path to represent the selected element. For subsets, the path Boolean array (equal to the candidate element length) indicates whether the candidate values for a particular location are selected (True and false).

Typical problems are: 77-combinations and 78-subsets.

2.2.3 Arrangement (permutation)

Arranging the questions is a little more complicated, of course, the templates mentioned above still apply, but there is a special problem with the combination and subset problems: Next permutation, which calculates the specified K-permutation, or given the K-k+1. The algorithm Design Manual provides two methods: Rank/unrank (Cantor unfold, Cantor Expansion) and incremental Change (increment method). The former can directly calculate the position of each element in the K-permutation, but because the k! factorial is used, special handling is required for larger integers. The increment rule is calculated according to the k+1 of section K. The detailed explanation of these two methods, please refer to the Soul machine in the following typical problems of the solution, written very clearly, in this will not elaborate ~

Typical problems are: 46-permutations (General method), 31-next permutation (increment method), 60-permutation Sequence (Cantor method).

2.2.4 Partition (Partition)

The generic template is still available, but the index of the delimited location is saved with the path array.

Typical problems are: 131-palindrome partitioning.

3. Exercise List 3.1 backtracking (backtracking) 3.1.1 Depth First (DFS)

62-unique Paths (Medium): A robot is located at the Top-left corner of a m x N grid (marked ' Start ' in the Diagra M below). The robot can only move either down or right at any point in time. The robot is trying-to-reach the bottom-right corner of the grid (marked ' Finish ' in the diagram below). How many possible unique paths is there?

63-unique Paths II (Medium): Follow up for ' Unique Paths ': Now consider if some obstacles is added to the grids. How many unique paths would there be? An obstacle and empty space are marked as 1 and 0 respectively in the grid.
For example, there are one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.
Note:m and N would be is at most 100.

70-climbing Stairs: You is climbing a stair case. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?

79-word Search (Medium): Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or V Ertically neighboring. The same letter cell is used more than once.
For example, Given board =
[
[' A ', ' B ', ' C ', ' E '],
[' s ', ' F ', ' C ', ' s '],
[' A ', ' D ', ' e ', ' e ']
]
Word = "abcced", returns True,
Word = "See", returns True,
Word = "ABCB", returns false.

93-restore IP Addresses (Medium): Given A string containing only digits, Restore it by returning all possible Val ID IP address combinations.
For Example:given "25525511135", Return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

Hint: There's a trap! About the treatment of 0. At the same time, in general, the conditional judgment of a recursive invocation can be moved into the base case after recursion, but this is an exception, because before recursion is called substring (), no advance judgment will be abnormal. But to be clear, I still keep the previous structure and do some processing in base case.

3.1.2 Breadth First (BFS)

127-word Ladder (Medium): Given, Words (Beginword and Endword), and a dictionary ' s Word list, find the length of shortest transformation sequence from Beginword to Endword, such that:
1.Only one letter can is changed at a time
2.Each Intermediate Word must exist in the word list
For example,
Given:
Beginword = "hit"
Endword = "COG"
WordList = ["Hot", "dot", "dog", "Lot", "log"]
As one shortest transformation is ' hit ', "Hot", "dot", "dog" and "cog", return its length 5.
Note:
Return 0 If there is no such transformation sequence.
All words has the same length.
All words contain only lowercase alphabetic characters.

Hint: Unlike other traversal, each level of the question can be deleted by deleting the accessed element, regardless of whether it loses access at the same level. Think about why? If this is not the case, then use BFS to search the same time to record each traversal path, because it is BFS so can not borrow recursion, this is too complex! This is also the problem-126 complexity.

126-word Ladder II : Given-Words (Beginword and Endword), and a dictionary ' s Word list, find all sh Ortest transformation Sequence (s) from Beginword to Endword, such this:
1.Only one letter can is changed at a time 2.Each Intermediate Word must exist in the word list
For example,
Given:
Beginword = ' hit '
Endword = "COG"
WordList = [' hot ', ' dot ', ' dog ', ' lot ', ' log ']
Return
[
[' Hit ', ' hot ', ' dot ', ' dog ', ' cog '],
["Hit ' Hot ', ' lot ', ' log ', ' cog ']
]
Note:
All words has the same length.
all words contain only lowercase alphabetic characters.

130-surrounded Regions (Medium): Given a 2D board containing ' x ' and ' O ', capture all regions surrounded by ' x '. A region was captured by flipping all ' O ' s into the ' X ' s in this surrounded region.
For example,
x x x x
X o o x
x x O X
x O x X
After running your function, the board should is:
x x x x
x x x x
x x x x
x O x X

Hint: With Dfs carefully realized, thought once can pass, results stuck in the big data set ... Through this example, we can understand how to implement backtracking with BFS, so as to have a deeper understanding of the nature of backtracking in multiple implementations. Although it can pass the judge, but the performance is still not good, because like the queue and path records and so on must have the article to do. But mainly used to understand the BFS, it can be passed.

3.2 Combination (combination)

17-letter combinations of a Phone number (Medium): Given A digit string, return all possible letter combinations That the number could represent. A mapping of Digit to letters (just as on the telephone buttons) is given below.
Input:digit string "23"
Output: ["Ad", "AE", "AF", "BD", "Be", "BF", "CD", "CE", "CF"].

77-combinations (Medium): Given, integers n and K, return all possible combinations of k numbers out of 1 ... n.
For example, If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[+],
[1,3],
[1,4],
]

22-generate Parentheses (Medium): Given n pairs of parentheses, write a function to Generate all combinations of Well-Formed parentheses.
For example, given n = 3, a solution set is:
[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]

Hint: Not simply the solution of the sub-problem stitching on the line, the merger of the problem is still quite complex.

3.3 Subset (subset)

78-subsets (Medium): Given a set of distinct integers, nums, return all possible subsets.
Note:the solution set must not contain duplicate subsets.
For example, If nums = [], a solution is:
[
[3],
[1],
[2],
[A],
[1,3],
[2,3],
[+],
[]
]

90-subsets II (Medium): Given A collection of integers that might contain duplicates, nums, return all possible s Ubsets.
Note:the solution set must not contain duplicate subsets.
For example, If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[+],
[]
]

39-combination Sum (Medium): Given A set of candidate numbers (C) and a target number (T), find all unique combin Ations in C where the candidate numbers sums to T. The same repeated number is chosen from C unlimited number of times.
Note:all numbers (including target) would be positive integers. The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and Target 7,
A Solution set is:
[
[7],
[2, 2, 3]
]

Hint: Although called combination, but this is actually a subset problem. Because duplicates are allowed, the subset question holds whether the bool array appears, to be extended to the number of occurrences of the int array. Several typical errors have been made in this extension problem:

    1. Because the sum is the subset of target and allows repetition, K does not have to terminate recursion to the last candidates. Online judge will time out if you don't prune it beforehand.
    2. In target=0 to save to the final result, do not modify the state array, in case of careless while (present[i]–> 0), it will affect the execution after backtracking, remember!
    3. A little trick: every time you judge target=0 to traverse the state array, you can use recursive time entry to avoid this consumption

40-combination Sum II (Medium): Given A collection of candidate numbers (C) and a target number (T), find all Uni Que combinations in C where the candidate numbers sums to T. Each number in C is used once in the combination.
Note:all numbers (including target) would be positive integers. The solution set must not contain duplicate combinations.
For example, given candidate set [1, 2, 7, 6, 1, 5] and Target 8,
A Solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

Hint: This topic on performance requirements than 1 high, not only to target==0 pruning, but also to target<0 pruning (the title said, all the numbers including Target is definitely positive), otherwise it will time out. Because before the topic 112-path sum was mistakenly cut the branch, because Path may be negative, so this question did not dare to superfluous. it seems that when pruning, how to cut, are learning!

3.4 Arrangement (permutation)

30-substring with concatenation of any Words (hard): You is given a string, s, and a list of Words, Words, that Is all of the same length. Find all starting indices of substring (s) in S that's a concatenation of each word in words exactly once and without any Intervening characters.
For example, given:s: "Barfoothefoobarman", Words: ["foo", "Bar"]
You should return the indices: [0,9]. (Order does not matter).

46-permutations (Medium): Given A collection of distinct numbers, return all possible permutations.
For example, [I/z] has the following permutations:
[
[A],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

47-permutations II (Medium): Given A collection of numbers that might contain duplicates, return all possible uni Que permutations.
For example, [1,1,2] has the following unique permutations:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

31-next permutation (Medium): Implement Next permutation, which rearranges numbers into the lexicographically NEX t greater permutation of numbers. If Such arrangement is not a possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must is in-place, do not allocate extra memory.
Here is some examples. Inputs is in the left-hand column and its corresponding outputs is in the right-hand column.
1,2,3→1,3,2
3,2,1→1,2,3
1,1,5→1,5,1

Hint: Note that because the elements can be equal, the first and second steps look for the I and J positions, taking into account equal conditions.

60-permutation Sequence (Medium): the set [,..., N] contains a total of n! Unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given N and K, return the kth permutation sequence.
Note:given n would be between 1 and 9 inclusive.

Hint: Using the so-called Cantor code, this question is more like a math game ...

89-gray Code (Medium): The Gray code is a binary numeral system where one of the successive values differ in only one bi T. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A Gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00-0
01-1
11-3
10-2
Note:for a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1] was also a valid gray code sequence according to the above definition.

3.5 Partitioning (Partition)

131-palindrome Partitioning (Medium): Given A string s, partition s such that every substring of the partition is A palindrome. Return all possible palindrome partitioning of S.
For example, given s = "AaB",
Return
[
["AA", "B"],
["A", "a", "B"]
]

Hint: Just recursion on one side, otherwise it's a split treatment!

131-palindrome Partitioning II (hard): Given A string s, partition s such that every substring of the partition I S a palindrome. Return the minimum cuts needed for a palindrome partitioning of S.
For example, given s = "AaB", Return 1 since the palindrome partitioning ["AA", "B"] could is produced using 1 cut.

Leetcode Classification Analysis: Combinatorial algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.