Knapsack

Poornima K S
Feb 2, 2022

Complexity: Polynomial time - O(sum * number of elements)

Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack.

Points to remember:

  • We need to initialize the initial value as zero.
  • Scale up the value of the dp array by so that we do not have out of bounds exception
  • The max value of the knapsack is always compared with the value with the one having same sum and with lesser element.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Poornima K S
Poornima K S

No responses yet

Write a response