问题描述

  • 地址

问题分析

  • 该题类似于排列问题,一个元素可以使用多次。而 LeetCode 518. Coin Change 2 类似于组合问题,同样一个元素可以使用多次,两题都是统计可能性的次数,因为该题还要考虑顺序,所以肯定比 518统计的次数多。
  • 画出递归树从递归函数入手,然后到记忆化搜索,然后到动态规划。注意,也可以先对数组进行排序来优化。
  • 该题中记忆化搜索要比动态规划运行时间更快,原因在于,记忆化搜索依旧是自顶向下,而动态规划是自底向上,举一个极端的例子,便是 nums = {100}, target = 100 若是自顶向下,两次递归即可。而动态规划需要从dp[0] 一直计算到dp[100],中间包含很多多余计算。
  • 记忆化搜素可以用数组,也可以用map,但map更加节省空间,并且,数组要注意初始化为何值才能保证与以后存入的值不冲突
  • 关于 Follow up :
    • 如果数组中也存在负数,那么便会有无数种结果,比如对于nums = {1, -1, 2 , -2},只要能形成0,那么便无数种结果,所以,只能限制每个元素用的数量,或者限制序列的长度。

经验教训

  • 关于排列与组合问题的区别,377与518,联系dfs时的排列组合。
  • 记忆化搜索与动态规划

代码实现

  • 递归(TLE)
    //递归解法// HashMap<Integer, Integer> map = new HashMap<>();public int combinationSum4(int[] nums, int target) {if (nums == null) {return 0;}int[] dp = new int[target + 1];return count(nums, target);}public int count(int[] nums, int remain) {if (remain == 0) {return 1;}int res = 0;for (int i = 0; i < nums.length; ++i) {if (remain >=  nums[i]) {res += count(nums, remain - nums[i], dp);}}return res;}
  • 记忆化搜索
    //记忆化搜索// HashMap<Integer, Integer> map = new HashMap<>();public int combinationSum4(int[] nums, int target) {if (nums == null) {return 0;}//Arrays.sort(nums);int[] dp = new int[target + 1];Arrays.fill(dp, -1);return count(nums, target, dp);}public int count(int[] nums, int remain, int[] dp) {if (remain == 0) {return 1;}if (dp[remain] != -1) {return dp[remain];}// if (map.containsKey(remain)) {//      return map.get(remain); // }int res = 0;for (int i = 0; i < nums.length; ++i) {//if (remain < nums[i]) {//   break;//}if (remain >=  nums[i]) {res += count(nums, remain - nums[i], dp);}}//map.put(remain, res);dp[remain] = res;return res;}
  • 动态规划
    //动态规划:未排序public int combinationSum4(int[] nums, int target) {if (nums == null) {return 0;}int[] dp = new int[target + 1];dp[0] = 1;for (int i = 1; i < dp.length; ++i) {int res = 0;for (int j = 0; j < nums.length; ++j) {if (nums[j] <= i) {res += dp[i - nums[j]];}}dp[i] = res;}return dp[target];}
  • 动态规划(排序)
    //动态规划:排序public int combinationSum4(int[] nums, int target) {if (nums == null) {return 0;}int[] dp = new int[target + 1];Arrays.sort(nums);dp[0] = 1;for (int i = 1; i < dp.length; ++i) {int res = 0;for (int j = 0; j < nums.length; ++j) {if (nums[j] > i) {break;}res += dp[i - nums[j]];}dp[i] = res;}return dp[target];}

LeetCode 377. Combination Sum IV相关推荐

  1. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  2. leetcode 377. Combination Sum IV | 377. 组合总和 Ⅳ(动态规划)

    题目 https://leetcode.com/problems/combination-sum-iv/ 题解 最近养成了上来直奔 Related Topics 的习惯- 确认过眼神,又是个 dp 问 ...

  3. 【DFS】LeetCode 39. Combination Sum

    LeetCode 39. Combination Sum Solution1: DFS,这个套路要熟记啊! class Solution { public:vector<vector<in ...

  4. LeetCode 653. Two Sum IV - Input is a BST--Python解法

    题目地址:Two Sum IV - Input is a BST - LeetCode Given a Binary Search Tree and a target number, return t ...

  5. LeetCode Combination Sum IV(动态规划)

    问题:给出一个数组nums和目标数target,问有多少组合形式 思路:用dp(i)表示目标数target的组合数.则有状态转移关系为dp(i)=sum(dp(i-nums[j])),其中i>= ...

  6. leetcode 216. Combination Sum III | 216. 组合总和 III(Java)

    题目 https://leetcode.com/problems/combination-sum-iii/ 题解 回溯法(back tracking)是一种选优搜索法.可以理解为通过选择不同的岔路口寻 ...

  7. leetcode 39. Combination Sum | 39. 组合总和(Java)

    题目 https://leetcode.com/problems/combination-sum/ 题解 不是最优解法. 对于每一个位置 i 上 的元素,分为选或不选两种情况. 遍历每一个位置,计算强 ...

  8. LeetCode 653. Two Sum IV - Input is a BST

    题目: Given a Binary Search Tree and a target number, return true if there exist two elements in the B ...

  9. 【DFS + backtracking】LeetCode 216. Combination Sum III

    Solution1:我的答案 DFS+backtracking,时间复杂度O(2m)=O(29)O(2m)=O(29)O(2^m)=O(2^9),空间复杂度O(k)O(k)O(k) class Sol ...

最新文章

  1. 深度学习(4)基础4 -- 神经网络架构激活函数过拟合处理
  2. 【AAAI 2020】NAS+目标检测:SM-NAS 论文解读
  3. 内存储器和cpu一起构成了计算机,计算机系统的组成
  4. 远程ubuntu虚拟机Tensorflow搭建 - 1 SSH连接
  5. java求数组和值_用java编写数组求和,array[]和ArrayList()?
  6. linux 字符串截取_第13篇:Linux防火墙的日志基本审计
  7. JUnit5 假设示例
  8. C++深度探索系列:智能指针(Smart Pointer) [一] (转)
  9. ap计算机科学ab,AP微积分AB_AP科目介绍|AP考试网
  10. 开源、个人博客等网站搭建、上云费用控、软件程序爱好者资源集锦
  11. python蜂鸣器_Python与硬件学习笔记:蜂鸣器(转)
  12. 如何用Intel主板集成的RAID控制器(Intel RST)实现硬盘提速与硬盘数据恢复
  13. SAP MM 采购订单中“Delivery Completed“ Indicator
  14. 演出遭遇枪击 前Pantera吉他手不幸身亡
  15. Cisco(37)——BGP的十三条选路原则演示
  16. C语言示例,三个骰子
  17. 从零开始使用AntDB
  18. ESP8266 复位 ets Jan 8 2013,rst cause:4, boot mode:(3,7)
  19. 最近‘张同学’在DY实在是太火了,忍不住用Python分析了一下他的dy评论数据,相信有朝一日我也能够爆火起来
  20. 常见的web安全问题有哪些

热门文章

  1. 输入有限个英文(小写)单词,单词可以重复(不统计数量),按照降序输出这些单词。
  2. 第20 章 Docker Swarm 集群实践
  3. GDCPC2016题解 by lby@SYSU | Asiimov
  4. Norflash 驱动
  5. 文本处理——用记事本打开xlsx
  6. 如何在ES6中判断类中是否包含某个属性和方法
  7. QT之qt4.8.6安装(详细教程)
  8. web前端之锋利的jQuery十一:综合开发,编写购物网首页
  9. 红尘有爱,盈花香满怀
  10. 赞丽生活、趣步的区块链电商模式解析