查看: 6105| 回复: 12
跳转到指定楼层
上一主题 下一主题
收起左侧

[动态规划] Jane Street的一道DP解题

 
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 ravenscar 于 2023-7-5 00:52 编辑

在本论坛某处看到的题目,但是在网上搜不到原题,最接近的是GeeksForGeeks,于是自己写了一下。


题目:


Given an array of integers, find the count of all subsets from the given array whose product is K.


Example:
{ 1, 1, 2, 2, 3, 4 }, target: 4, output: 8

The subsets are:
4,
1,4
1,4
1,1,4
2,2
1,2,2
1,2,2
1,1,2,2
  1. </blockquote></div><div class="blockcode"><blockquote>// Bottom-up DP:

  2. // The DP 2D array:
  3. // 0 1 0 0 0
  4. // 0 3 0 0 0
  5. // 0 3 4 0 0
  6. // 0 3 8 0 4
  7. // 0 3 8 4 4
  8. // 0 3 8 4 8

  9. int Search(const std::vector<int>& nums, int target) {
  10.     auto dp = std::vector<std::vector<int>>(nums.size(), std::vector<int>(target+1, 0));

  11.     for (int i=0; i<nums.size(); ++i) {
  12.         for (int j=1; j<target+1; ++j) {
  13.             int count = 0;
  14.             // +1 if the number is equal to the target.
  15.             if (nums[i] == j) {
  16.                 count++;
  17.             }
  18.             if (i>0) {
  19.                 // If the target can be divided by the current number, we find how many subsets for target / the_current_number.
  20.                 if (j % nums[i] == 0) {
  21.                     count += dp[i-1][j / nums[i]];
  22.                 }
  23.                 // Finally, add the number of subsets without the current number.
  24.                 count += dp[i-1][j];
  25.             }
  26.             dp[i][j] = count;
  27.         }
  28.     }
  29.     return dp[nums.size()-1][target];
  30. }
复制代码

评分

参与人数 2大米 +2 收起 理由
小亩_xzmpq6m + 1 赞一个
zjy0418 + 1 谢谢分享!

查看全部评分


上一篇:求一个99会员的link
下一篇:LC周赛疯了.....
全局:
  1. class Solution:
  2.     def jane_street_bottom_up(self, nums: list, target: int) -> int:
  3.         """
  4.              0  1  2  3  4  target
  5.         0(1) 0  1  0  0  0
  6.         1(1) 0  3  0  0  0
  7.         2(2) 0  3  4  0  0
  8.         3(0) 8  3  4  0  0
  9.         4(3) 16 3  4  4  0
  10.         5(4) 32 3  4  4  0
  11.         index
  12.         """
  13.         table = [[0] * (target + 1) for _ in range(len(nums))]
  14.         for i in range(len(nums)):  # target
  15.             for j in range(target + 1):  # index start from 0
  16.                 if nums[i] == j:
  17.                     table[i][j] += 1
  18.                 if i > 0:
  19.                     table[i][j] += table[i - 1][j]  # the cases without nums[i]
  20.                     # the cases with nums[i]
  21.                     # two situations: 0 in the nums and target is 0
  22.                     if nums[i] == 0:
  23.                         if j == 0:
  24.                             table[i][j] += 2 ** i - 1
  25.                     else:
  26.                         if j % nums[i] == 0:
  27.                             table[i][j] += table[i - 1][j // nums[i]]
  28.         # print(table)
  29.         return table[len(nums) - 1][target]
复制代码

补充内容 (2023-07-05 23:17 +08:00):

代码片段仅仅加了一些可能性,即当target可能是0或数组中也可能有0的情况。相当于增加了一点cornor case。例如:nums = [1,1,2,0,3,4], target = 0.则可返回32。这个边缘情况也可以放置在主要代码片段外面,专门写出来。

评分

参与人数 1大米 +1 收起 理由
337845818 + 1 数组中有0还需要考虑吗

查看全部评分

回复

使用道具 举报

推荐
Javery 2023-7-5 11:36:06 | 只看该作者
全局:
这题有个细节,有没有0存在的可能性?
回复

使用道具 举报

全局:
背包問題啊
回复

使用道具 举报

🔗
ourgit 2023-7-5 10:22:19 | 只看该作者
全局:
leetcode上面这种背包题目很多,各种变形题
回复

使用道具 举报

全局:
Javery 发表于 2023-07-04 20:36:06
这题有个细节,有没有0存在的可能性?
子集,不是货物无限的背包问题
回复

使用道具 举报

🔗
337845818 2023-7-6 07:45:12 | 只看该作者
全局:
确实是背包, dp[len][target+1] 行吗?
题里没说ary大小,也没说target大小。1e9的target能做吗,如果用除法呢?
回复

使用道具 举报

🔗
337845818 2023-7-6 07:46:21 | 只看该作者
全局:
Javery 发表于 2023-7-4 21:21
补充内容 (2023-07-05 23:17 +08:00):

代码片段仅仅加了一些可能性,即当target可 ...
数组中有0还需要考虑吗
回复

使用道具 举报

全局:
想问一下既然是subset,暴力backtracking是不是也可以,就是复杂度有点高
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表