当前位置:网站首页>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]
边栏推荐
- In the future, how long will robots or AI have human creativity?
- Poverty has nothing to do with suffering
- WinForm (II) advanced WinForm and use of complex controls
- 微信小程序,购买商品属性自动换行,固定div个数,超出部分自动换行
- How to purchase 25g optical network card
- Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
- Huawei equipment configures local virtual private network mutual access
- Section III: structural characteristics of cement concrete pavement
- Emnlp2021 𞓜 a small number of data relation extraction papers of deepblueai team were hired
- 智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?
猜你喜欢

2021 iccv paper sharing - occlusion boundary detection

自定义View基础之Layout

MySQL string to array, merge result set, and convert to array

Top 100 video information of station B

JVM tuning 6: GC log analysis and constant pool explanation

jvm调优六:GC日志分析和常量池详解

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

Analysis while experiment - a little optimization of memory leakage in kotlin

Huawei equipment configures local virtual private network mutual access

1. use alicloud object OSS (basic)
随机推荐
English digital converter
Zed2 running vins-mono preliminary test
Wechat applet, automatic line feed for purchasing commodity attributes, fixed number of divs, automatic line feed for excess parts
Network adapter purchase guide
华为设备配置通过GRE接入虚拟专用网
Opencv learning path (2-2) -- Deep parsing namedwindow function
力扣(LeetCode)161. 相隔为 1 的编辑距离(2022.06.10)
使用acme.sh自动申请免费SSL证书
Analyzing while experimenting - memory leakage caused by non static inner classes
WinForm (II) advanced WinForm and use of complex controls
White Gaussian noise (WGN)
【入门级基础】Node基础知识总结
Share 𞓜 jointly pre training transformers on unpaired images and text
Common methods of tool class objectutil
Section I: classification and classification of urban roads
Concurrent search set
选择数字资产托管人时,要问的 6 个问题
What is the difference between a wired network card and a wireless network card?
Technology | image motion drive interpretation of first order motion model
Tightly coupled laser vision inertial navigation slam system: paper notes_ S2D. 66_ ICRA_ 2021_ LVI-SAM