首页 > 其他分享 >leetcode 377. 组合总和 Ⅳ(dp)

leetcode 377. 组合总和 Ⅳ(dp)

时间:2024-06-03 21:15:06浏览次数:19  
标签:std cout int long dp leetcode 377

377. 组合总和 Ⅳ - 力扣(LeetCode)

dp,跟完全背包反着来,可以当作是爬楼梯来做,相当于每次爬的楼梯数是从数组种选的。

 1 #define IO std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
 2 #define bug(x) cout<<#x<<" is "<<x<<endl;
 3 #include<bits/stdc++.h>
 4 using namespace std;
 5 typedef long long ll;
 6 class Solution {
 7 public:
 8     int combinationSum4(vector<int>& nums, int target) {
 9         vector<int>d(1005,0);
10         d[0]=1;
11         for(int i=0;i<=target;i++){
12             for(int j=0;j<nums.size();j++){
13                 if(i-nums[j]>=0&&d[i]<INT_MAX-d[i-nums[j]])d[i]+=d[i-nums[j]];
14             }
15         }
16         return d[target];
17     }
18 };
19 int main(){
20     vector<int>v={10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380,390,400,410,420,430,440,450,460,470,480,490,500,510,520,530,540,550,560,570,580,590,600,610,620,630,640,650,660,670,680,690,700,710,720,730,740,750,760,770,780,790,800,810,820,830,840,850,860,870,880,890,900,910,920,930,940,950,960,970,980,990,111};
21     Solution A;
22     cout<<A.combinationSum4(v,999);
23 }

 

       

标签:std,cout,int,long,dp,leetcode,377
From: https://www.cnblogs.com/ccsu-kid/p/18229644

相关文章

  • Floyd判圈算法 leetcode
    龟兔赛跑/Floyd判圈算法概述判断一个链表是否存在环画图演示两个指针相遇的情况:查找链表中环的首个节点在这里插入图片描述数学公式表示为:(对应力扣142.环形链表II,141.环形链表I)判断一个链表是否存在环龟兔赛跑/Floyd判圈算法转换成判断链表是否存......
  • 一文读懂GDPR
    GDPR将对人们的网络足迹、使用的APP和服务如何保护或利用这些数据产生重大影响。下面我们将对有关GDPR人们最关心的问题进行解读。 GDPR是什么?一般数据保护条例(GeneralDataProtectionRegulation)是一项全面的法律,赋予了欧盟居民对个人数据的更多控制权,并试图澄清在线服......
  • 背包 dp 学习笔记
    背包类问题是动态规划中的一类重要问题1.01背包有\(n\)件物品和一个容量为\(v\)的背包。第\(i\)件物品的费用是\(c_i\),价值是\(w_i\)。求解将哪些物品装入背包可使价值总和最大。1.1基本思路我们首先定义此问题的dp状态\(f_{i,j}\)表示前\(i\)件物品放入一个......
  • LeetCode 1168. Optimize Water Distribution in a Village
    原题链接在这里:https://leetcode.com/problems/optimize-water-distribution-in-a-village/description/题目:Thereare n housesinavillage.Wewanttosupplywaterforallthehousesbybuildingwellsandlayingpipes.Foreachhouse i,wecaneitherbuildaw......
  • LeetCode 720. Longest Word in Dictionary
    原题链接在这里:https://leetcode.com/problems/longest-word-in-dictionary/description/题目:Givenanarrayofstrings words representinganEnglishDictionary,return thelongestwordin words thatcanbebuiltonecharacteratatimebyotherwordsin wor......
  • leetcode第263题:丑数
    丑数的因子只能是2,3,5。但是可能有多个2,多个3,多个5.因此需要循环地除以2、3、5.publicclassSolution{publicboolIsUgly(intn){if(n<=0){returnfalse;}int[]factors={2,3,5};for(inti=0;i......
  • leetcode第1281题: 整数的各位积和之差
    publicclassSolution{publicintSubtractProductAndSum(intn){intsum=0;intji=1;while(n>0){intnum=n%10;sum+=num;ji*=num;n/=10;}returnji-sum;......
  • PDPS二次开发插件流程
    PDPS二次开发插件流程一.第一步通过C#创建插件dll1.在本地安装PDPS的安装目录下找到eMpower下的Tecnomatix.Engineering.dll,Tecnomatix.Engineering.Ui.dll2.在vs中新建winform窗体,引用以上目录下的两个dll文件3.新建一个类文件例如叫FristTestPlugin,继承Engineering下的TxBut......
  • LeetCode 1151. 最少交换次数来组合所有的 1
    1151.最少交换次数来组合所有的1给出一个二进制数组 data,你需要通过交换位置,将数组中 任何位置 上的1组合到一起,并返回所有可能中所需 最少的交换次数。示例1:输入:data=[1,0,1,0,1]输出:1解释:有三种可能的方法可以把所有的1组合在一起:[1,1,1,0,0],交换......
  • LeetCode 1411. Number of Ways to Paint N × 3 Grid
    原题链接在这里:https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/description/题目:Youhavea grid ofsize nx3 andyouwanttopainteachcellofthegridwithexactlyoneofthethreecolors: Red, Yellow, or Green whilemakingsuretha......