This problem is simple. You have N items numbered 1 to N. Each item has a weight Wi But wait, you might be wondering why the items don't have values. Well, that's because this isn't your normal knapsack. This is an even more basic knapsack. There's no need to worry about values, because you will be given the capacity of your knapsack. Instead of maximizing a value, just count how many ways you can fill your knapsack without exceeding capacity. For example, if you have 3 items with weights 3, 5, and 7 and your knapsack has a capacity 9, there are 5 ways to fill the knapsack as follows:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Information is present in the screenshot and below. Based on that need help in solving the code for this problem in python. The time complexity has to be as less as possible. Apply dynamic programming and memoization. Do not use recursion. Make sure ALL test cases return expected outputs.

Hint: Approach the problem with 2D Dynamic Programming.

Output Format
Output one line containing a single integer containing the answer as described in the problem statement. This number must be output mod 109 + 7.

Sample Input 0:
3 9
3
5
7

Sample Output 0:
5

Sample Input 1:
3 23
15
16
15

Sample Output 1:
4

Sample Input 2:
3 6
1
2
3

Sample Output 2:
8

The actual code:

def solve(n,c,ws):
    # compute and return answer here
    # place 2 nested for loops here
    
MOD = 1000000007
n, c = list(map(int,input().rstrip().split(" ")))
ws = [int(input().rstrip()) for i in range(n)]
print(solve(n,c,ws))
 

 

This problem is simple. You have N items numbered 1 to N. Each item has a weight Wį.
But wait, you might be wondering why the items don't have values. Well, that's because
this isn't your normal knapsack. This is an even more basic knapsack. There's no need to
worry about values, because you will be given the capacity of your knapsack. Instead of
maximizing a value, just count how many ways you can fill your knapsack without
exceeding capacity.
For example, if you have 3 items with weights 3, 5, and 7 and your knapsack has a
capacity 9, there are 5 ways to fill the knapsack as follows:
Empty knapsack
●
• 3
• 5
• 7
• 3 + 5
This number may be very large, so print it mod 10⁹ +7.
Input Format
Input contains a single test case starting with a line containing two space-separated
integers N and C, indicating the number of items and knapsack capacity respectively.
N lines follow, the ith containing a single integer Wi, the weight of the ith line.
Constraints
1 ≤N, W; ≤ 2.10³
i
0≤ C≤2·10³
Transcribed Image Text:This problem is simple. You have N items numbered 1 to N. Each item has a weight Wį. But wait, you might be wondering why the items don't have values. Well, that's because this isn't your normal knapsack. This is an even more basic knapsack. There's no need to worry about values, because you will be given the capacity of your knapsack. Instead of maximizing a value, just count how many ways you can fill your knapsack without exceeding capacity. For example, if you have 3 items with weights 3, 5, and 7 and your knapsack has a capacity 9, there are 5 ways to fill the knapsack as follows: Empty knapsack ● • 3 • 5 • 7 • 3 + 5 This number may be very large, so print it mod 10⁹ +7. Input Format Input contains a single test case starting with a line containing two space-separated integers N and C, indicating the number of items and knapsack capacity respectively. N lines follow, the ith containing a single integer Wi, the weight of the ith line. Constraints 1 ≤N, W; ≤ 2.10³ i 0≤ C≤2·10³
Expert Solution
steps

Step by step

Solved in 5 steps with 1 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education