当前位置:网站首页>Combination sum Ⅳ -- leetcode exercise
Combination sum Ⅳ -- leetcode exercise
2022-06-11 05:21:00 【Food can be very delicious】
I haven't done a problem for a long time , More dishes 5555
Title Description
Here you are Different An array of integers nums , And a target integer target . Please start from nums Find and return the sum of target The number of element combinations of .
The data of the questions ensure that the answers are in line with 32 Bit integer range .
example
Input :nums = [1,2,3], target = 4
Output :7
explain :
All possible combinations are :
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Please note that , Different sequences are considered as different combinations .
Ideas
Be careful : What the topic requires is that different order is also a combination
The easier way to think of is DFS, But the test timed out , I won't go into details here .
Dynamic programming
Create a length of target( The goal is )+1 Array of , Record each i(0<=i<=target) How many combinations of values of .
If the goal is 0, that dp[0]=1, Don't choose a number .
You can put nums Array sorting , Make two layers for loop , The outer layer is 1 ~ target, The inner layer is 0 ~ len(nums)-1.
dp[i]=dp[i-nums[0]]+dp[i-nums[1]]+dp[i-nums[2]]+……+dp[i-nums[j]] (nums[j+1]>i)
Code
class Solution:
def combinationSum4(self, nums: List[int], target: int) -> int:
nums.sort()
dp=[0]*(target+1)
dp[0]=1
for i in range(1,len(dp)):
for j in range(len(nums)):
if nums[j]>i:
break
else:
dp[i]+=dp[i-nums[j]]
return dp[-1]
边栏推荐
- go MPG
- Analysis while experiment - a little optimization of memory leakage in kotlin
- 智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?
- KD-Tree and LSH
- Using keras to build the basic model yingtailing flower
- Tightly coupled laser vision inertial navigation slam system: paper notes_ S2D. 66_ ICRA_ 2021_ LVI-SAM
- Paper reproduction: pare
- Deep extension technology: intelligent OCR recognition technology based on deep learning has great potential
- Solving graph problems with union search set
- [project - what are the supporting materials in the annexes? (18 kinds of 2000 word summary)] project plan of innovation and entrepreneurship competition and supporting materials of Challenge Cup entr
猜你喜欢

Simple linear regression of sklearn series

Wechat applet uploads the data obtained from database 1 to database 2

GAMES101作业7-Path Tracing实现过程&代码详细解读

New product pre-sale: 25g optical network card based on Intel 800 series is coming

(15) Infrared communication

在未来,机器人或 AI 还有多久才能具备人类的创造力?

How much current can PCB wiring carry

Paper reproduction: pare
![[aaai 2021 timing action nomination generation] detailed interpretation of bsn++ long article](/img/28/d69a7583036a2076facffcf9098d7e.jpg)
[aaai 2021 timing action nomination generation] detailed interpretation of bsn++ long article

6 questions to ask when selecting a digital asset custodian
随机推荐
22、生成括号
Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology
White Gaussian noise (WGN)
Zed2 camera manual
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
Section III: structural characteristics of cement concrete pavement
Section IV: composition and materials of asphalt mixture (2) -- main materials of asphalt
Tianchi - student test score forecast
Combien de courant le câblage des PCB peut - il supporter?
推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用
Huawei equipment configuration MCE
[project - what are the supporting materials in the annexes? (18 kinds of 2000 word summary)] project plan of innovation and entrepreneurship competition and supporting materials of Challenge Cup entr
Opencv learning path (2-1) -- Deep parsing imread function
[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision
The central rural work conference has released important signals. Ten ways for AI technology to help agriculture can be expected in the future
Concurrent search set
In the future, how long will robots or AI have human creativity?
Huawei equipment configures local virtual private network mutual access
JVM tuning 6: GC log analysis and constant pool explanation
Comparison of gigabit network card chips: who is better, a rising star or a Jianghu elder?