Files
JavaScript/Dynamic-Programming/ZeroOneKnapsack.js
Roland Hummel 86d333ee94 feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
2023-10-04 02:38:19 +05:30

76 lines
1.9 KiB
JavaScript

/**
* A Dynamic Programming based solution for calculating Zero One Knapsack
* https://en.wikipedia.org/wiki/Knapsack_problem
*/
const zeroOneKnapsack = (arr, n, cap, cache) => {
if (cap === 0 || n === 0) {
cache[n][cap] = 0
return cache[n][cap]
}
if (cache[n][cap] !== -1) {
return cache[n][cap]
}
if (arr[n - 1][0] <= cap) {
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)
)
return cache[n][cap]
} else {
cache[n][cap] = zeroOneKnapsack(arr, n - 1, cap, cache)
return cache[n][cap]
}
}
const example = () => {
/*
Problem Statement:
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.
You have to cherry pick the artifact in order to maximize the total value of the artifacts you stole.
Link for the Problem: https://www.hackerrank.com/contests/srin-aadc03/challenges/classic-01-knapsack
*/
let input = `1
4 5
1 8
2 4
3 0
2 5
2 3`
input = input.trim().split('\n')
input.shift()
const length = input.length
const output = []
let i = 0
while (i < length) {
const cap = Number(input[i].trim().split(' ')[0])
const currlen = Number(input[i].trim().split(' ')[1])
let j = i + 1
const arr = []
while (j <= i + currlen) {
arr.push(input[j])
j++
}
const newArr = arr.map((e) => e.trim().split(' ').map(Number))
const cache = []
for (let i = 0; i <= currlen; i++) {
const temp = []
for (let j = 0; j <= cap; j++) {
temp.push(-1)
}
cache.push(temp)
}
const result = zeroOneKnapsack(newArr, currlen, cap, cache)
output.push(result)
i += currlen + 1
}
return output
}
export { zeroOneKnapsack, example }