当前位置:网站首页>leetcode518,377
leetcode518,377
2022-07-05 00:23:00 【The left wheel of fate】
List of articles
518. Change for II
Give you an array of integers coins Coins of different denominations , Give another integer amount Represents the total amount .
Please calculate and return the number of coin combinations that can be rounded up to the total amount . If no combination of coins can make up the total amount , return 0 .
Suppose that there are infinite coins of each denomination .
The subject data ensure that the results meet 32 Bit signed integer .
Example 1:
Input :amount = 5, coins = [1, 2, 5]
Output :4
explain : There are four ways to make up the total amount :
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
Example 2:
Input :amount = 3, coins = [2]
Output :0
explain : Use only face value 2 We can't make up the total amount of 3 .
Example 3:
Input :amount = 10, coins = [10]
Output :1
Tips :
1 <= coins.length <= 300
1 <= coins[i] <= 5000
coins All the values in Different from each other
0 <= amount <= 5000
analysis
- If you want to calculate every possible set , You need a backtracking algorithm , If only counting , Dynamic planning .
- Abstract problem : Completely backpack ( Because a number can be taken many times )
- Find the total number : Combination question ,
dp[j] = dp[j-nums[i]]
- initialization :
dp[0] = 1
- traversal order , Double forward , Order doesn't matter
- dp[j] Express : Capacity of j My backpack has dp[j] A way to fill your backpack
Code
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
# amount: Indicates the maximum capacity of the backpack
# coins: Indicates an item
dp = [0] * (amount+1) # dp[j] Indicates that the full capacity is j The possibility of backpacking
dp[0] = 1
for i in coins: # Traverse the items
for j in range(i,amount+1): # Traverse the backpack
dp[j] += dp[j-i]
return dp[amount]
Through screenshots
377. Combinatorial summation Ⅳ
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 1:
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 .
Example 2:
Input :nums = [9], target = 3
Output :0
Tips :
1 <= nums.length <= 200
1 <= nums[i] <= 1000
nums All elements in Different from each other
1 <= target <= 1000
analysis
- Abstract problem : Completely backpack ( Because a number can be taken many times )
- Find the total number : Here is the case output , Knowing is a permutation problem
- initialization :
dp[0] = 1
- traversal order , We said that the complete knapsack problem is generally double positive order , There is no difference between the inner and outer layers of items and backpacks . however , In this question , The backpack must be on the outer layer , Items are inside . Think about it a little bit , Arrangement is the case of different order , If you put the object on the outer layer , Anyway, the order of items is fixed . So put the items on the inner layer , The item is traversed many times , In a different order .
- dp[j] Express : Capacity of j My backpack has dp[j] A way to fill your backpack .
- Because it is a forward traversal , Pay attention to the starting position , But since we started with backpacks , So I can't determine which position is more suitable at the beginning , So after the double cycle, we must judge the relationship between the size of the item and the backpack .
Code
class Solution:
def combinationSum4(self, nums: List[int], target: int) -> int:
# Completely backpack
# nums: goods
# target: The maximum capacity of the backpack
# dp[j]: The capacity of the backpack is j Number of permutations of
dp = [0] *(target+1)
dp[0] = 1
for i in range(1,target+1): # Traverse the backpack
for j in range(len(nums)): # Traverse the items
if nums[j] <= i: # When items can be put into backpacks
dp[i] += dp[i-nums[j]]
return dp[target]
Through screenshots
If there is a mistake , Please correct me , Welcome to exchange , thank you *(・ω・)ノ
边栏推荐
- XML的解析
- Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
- 微服务(Microservice)那点事儿
- 人脸识别5- insight-face-paddle-代码实战笔记
- Using fast parsing intranet penetration to realize zero cost self built website
- URLs and URIs
- 2022.07.03(LC_6109_知道秘密的人数)
- Summer challenge brings you to play harmoniyos multi terminal piano performance
- 【北京大学】Tensorflow2.0-1-开篇
- P3304 [sdoi2013] diameter (diameter of tree)
猜你喜欢
JS how to realize array to tree
巩固表达式C# 案例简单变量运算
Two numbers replace each other
How to do the project of computer remote company in foreign Internet?
What is the difference between port mapping and port forwarding
[path planning] RRT adds dynamic model for trajectory planning
端口映射和端口转发区别是什么
js如何实现数组转树
How many triangles are there in the golden K-line diagram?
Significance of acrel EMS integrated energy efficiency platform in campus construction
随机推荐
跨域请求
[IELTS reading] Wang Xiwei reading P4 (matching1)
P4281 [ahoi2008] emergency assembly / gathering (LCA)
模板的进阶
Verilog tutorial (11) initial block in Verilog
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
Cross domain request
abc 258 G - Triangle(bitset)
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
P3304 [SDOI2013]直径(树的直径)
【C】(笔试题)指针与数组,指针
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
使用快解析搭建自己的minecraft服务器
基于三维gis平台的消防系统运用
雅思考试流程、需要具体注意些什么、怎么复习?
[Peking University] tensorflow2.0-1-opening
【路径规划】RRT增加动力模型进行轨迹规划
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
js如何实现数组转树