Skip to main content
Fix code indentation and punctuation. [Review: don't modify code snippets. Style concerns should be addressed in an answer.]
Source Link
Daniel
  • 4.6k
  • 2
  • 18
  • 40

This is not the classical backpack problem. Below, the problem that you want to try to solve.

You have a backpack, and in front of you there are n bags. Let us say three bags with different materials. E.g.: One for gold, one for silver, and another for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
        index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
        index++;
        partialWeight=0;
}

This is not the classical backpack problem. Below, the problem that you want to try to solve.

You have a backpack, and in front of you there are n bags. Let us say three bags with different materials. E.g.: One for gold, one for silver, and another for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
    index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
    index++;
    partialWeight=0;
}

This is not the classical backpack problem. Below, the problem that you want to try to solve.

You have a backpack, and in front of you there are n bags. Let us say three bags with different materials. E.g.: One for gold, one for silver, and another for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
        index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
        index++;
        partialWeight=0;
}

This is not the classical backpack problem. Below, the problem that you want to try to solve.

You have a backpack, and in front of you, there are n bags, let. Let us say three bags with different materials. E.g.: One for gold, one for silver, and otheranother for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
        index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
        index++;
        partialWeight=0;
    }

This is not the classical backpack problem. Below, the problem that you want to try to solve.

You have a backpack and in front of you, there are n bags, let us say three bags with different materials. E.g.: One for gold, one for silver, and other for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
        index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
        index++;
        partialWeight=0;
    }

This is not the classical backpack problem. Below, the problem that you want to try to solve.

You have a backpack, and in front of you there are n bags. Let us say three bags with different materials. E.g.: One for gold, one for silver, and another for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
    index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
    index++;
    partialWeight=0;
}
added 1 character in body
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

This is not the classical backpack problem. BellowBelow, the problem that you want to try to solve.

You have a backpack and in front of you, there isare n bags, let us say three bags with different materials. E.g.: One for gold, one for silver, and other for copper. Each bag contains piecespieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
        index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeightpartialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
        index++;
        partialWeight=0;
    }

This is not the classical backpack problem. Bellow, the problem that you want to try to solve.

You have a backpack and in front of you, there is n bags, let us say three bags with different materials. E.g.: One for gold, one for silver, and other for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
        index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
        index++;
        partialWeight=0;
    }

This is not the classical backpack problem. Below, the problem that you want to try to solve.

You have a backpack and in front of you, there are n bags, let us say three bags with different materials. E.g.: One for gold, one for silver, and other for copper. Each bag contains pieces of 1 unit (let's say 1g) for its material. You want to fill up the backpack in the way that you have the maximum value for the capacity of your backpack.

However, even with this specific proposition, there is a problem with the fragment below:

if (totalWeight === sorted[index].weight) {
        index++;
}

For the first item, it would make sense, but then ... it does not. Maybe you should use another variable for partialWeight of each material. Ex:

totalWeight++;
partialWeight++;
if (partialWeight === sorted[index].weight) {
        index++;
        partialWeight=0;
    }
added 30 characters in body
Source Link
rdllopes
  • 868
  • 4
  • 15
Loading
Source Link
rdllopes
  • 868
  • 4
  • 15
Loading