Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does python find out the number of combinations?

Shulou Source: shulou.com Published: 2022-05-31 14:07:57 10月01日 Update

This article mainly introduces the relevant knowledge of "how to find out the number of combinations in python". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to find out the number of combinations in python" can help you solve the problem.

Topic: combination summation

Given an array of positive integers with no duplicate numbers, find out the number of combinations of positive integers with a given target.

Example:

Nums = [1,2,3]

Target = 4

All possible combinations are:

(1, 1, 1, 1)

(1, 1, 2)

(1, 2, 1)

(1, 3)

(2, 1, 1)

(2, 2)

(3, 1)

Note that sequences with different orders are treated as different combinations.

So the output is 7.

Solve the problem:

1. Dp problem. Dp [I] = sum (DP [I-nums [j])), where nums [j]

< i。 代码 class Solution: def combinationSum4(self, nums: List[int], target: int) ->

Int:

Nums.sort ()

Dp = [0] * (target + 1)

For i in range (1, target + 1):

For n in nums:

If n > I:

Break

If n = = I:

Dp [I] + = 1

Else:

Dp [I] + = dp [I-n]

Return dp [- 1]

This is the end of the content about "how to find out the number of combinations by python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Combinations numbers differences knowledge integers industries problems practicality code content practicality practice sequences summation numbers arrays articles methods more cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Apple Redmi Huawei