Leetcode-combinations Review and review permutations and combinations

Source: Internet
Author: User

Combinations

Test instructions

Generates an array of length k from 1 to n according to the given N and K

Example:

N=4 k=2

[1, 2],
[1, 3],
[1, 4],
[2, 1],
[2, 3],
[2, 4],
[3, 1],
[3, 2],
[3, 4],
[4, 1],
[4, 2],
[4, 3]]

Solving:

Normally, we usually think of it by using recursion to generate a combination in the form of enumerations. First, take a number from the given range, combine it with each of the remaining numbers, and then merge each number that does not exist in the combination on each combination, sequentially merging, and eventually generating the permutations we need.

But I found that using this method to commit on Leetcode would prompt a timeout. Later I found that we could actually use the formula in the permutation combination:

C (n,k) = C (n-1,k) + C (n-1, k-1)

  The intuitive interpretation of this formula is that the number of permutation combinations with length k in the range n can be converted into the sum of the number of permutations in the range n-1 and the number of n-1 in the range of length k-1.

It sounds like it's not just around the mouth, but it seems that the formula and the purpose we're trying to achieve seem to be of little use. In fact, we can easily understand a different angle.

We ask for a range of n-length k arrangement combinations, if we can find the range n-1 length k permutation combination, then we also need to include the number N of the length of the combination of K, but the range n-1 length of k-1

The permutation is exactly the same as the number n, so that the permutation is exactly what the C (n-1,k) difference is. So our code can be written as:

Class solution (Object):    def combine (self, n, k): "" "        : Type n:int        : Type k:int        : rtype:list[list[ INT]] "" "        if n = = k:            return [Range (1, n+1)]        if k = = 1:            return [[item] for item in range (1, n+1)]        ret = Self.combine (n-1, k) Other        = Self.combine (n-1, k-1) for item in other        :            item.append (n)        ret + = OT Her        return ret

If you want our code to appear more pythonic, we can also write:

Class solution (Object):    def combine (self, n, k): "" "        : Type n:int        : Type k:int        : rtype:list[list[ INT]] "" "        if n = = k:            return [Range (1, n+1)]        if k = = 1:            return [[item] for item in range (1, n+1)]        Return Self.combine (n-1, k) + [item + [n] for item in Self.combine (N-1, k-1)]        

Leetcode-combinations Review and review permutations and combinations

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.