Skip to content

Commit c9169dc

Browse files
0 1 knapsack (#408)
* Added Longest Common Subsequence * Renamed the File * Optimized the code * Optimized the code * Changed some styles as per the rule * Again some style fixed * Added Longest Increasing Subsequence program to the list * Style changed * Added 0-1-Knapsack Problem * Style Changed as per guidelines * Update ZeroOneKnapsack.js * Delete LongestCommonSubsequence.js * Delete LongestIncreasingSubsequence.js Co-authored-by: vinayak <itssvinayak@gmail.com>
1 parent fa01faf commit c9169dc

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* A Dynamic Programming based solution for calculating Zero One Knapsack
3+
* https://en.wikipedia.org/wiki/Knapsack_problem
4+
*/
5+
6+
const zeroOneKnapsack = (arr, n, cap, cache) => {
7+
if (cap === 0 || n === 0) {
8+
cache[n][cap] = 0
9+
return cache[n][cap]
10+
}
11+
if (cache[n][cap] !== -1) {
12+
return cache[n][cap]
13+
}
14+
if (arr[n - 1][0] <= cap) {
15+
cache[n][cap] = Math.max(arr[n - 1][1] + zeroOneKnapsack(arr, n - 1, cap - arr[n - 1][0], cache), zeroOneKnapsack(arr, n - 1, cap, cache))
16+
return cache[n][cap]
17+
} else {
18+
cache[n][cap] = zeroOneKnapsack(arr, n - 1, cap, cache)
19+
return cache[n][cap]
20+
}
21+
}
22+
23+
const main = () => {
24+
/*
25+
Problem Statement:
26+
You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity.
27+
You have to cherry pick the artifact in order to maximize the total value of the artifacts you stole.
28+
29+
Link for the Problem: https://www.hackerrank.com/contests/srin-aadc03/challenges/classic-01-knapsack
30+
*/
31+
let input = `1
32+
4 5
33+
1 8
34+
2 4
35+
3 0
36+
2 5
37+
2 3`
38+
39+
input = input.trim().split('\n')
40+
input.shift()
41+
const length = input.length
42+
43+
let i = 0
44+
while (i < length) {
45+
const cap = Number(input[i].trim().split(' ')[0])
46+
const currlen = Number(input[i].trim().split(' ')[1])
47+
let j = i + 1
48+
const arr = []
49+
while (j <= i + currlen) {
50+
arr.push(input[j])
51+
j++
52+
}
53+
const newArr = []
54+
arr.map(e => {
55+
newArr.push(e.trim().split(' ').map(Number))
56+
})
57+
const cache = []
58+
for (let i = 0; i <= currlen; i++) {
59+
const temp = []
60+
for (let j = 0; j <= cap; j++) {
61+
temp.push(-1)
62+
}
63+
cache.push(temp)
64+
}
65+
const result = zeroOneKnapsack(newArr, currlen, cap, cache)
66+
console.log(result)
67+
i += currlen + 1
68+
}
69+
}
70+
71+
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy