Skip to content

Commit

Permalink
updated solution;
Browse files Browse the repository at this point in the history
  • Loading branch information
Mintas committed Mar 3, 2016
1 parent 27a9ce6 commit 4901dc0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ private static void printSolution(JackSparrowHelper jackSparrowHelper) {

System.out.println(abr.calculateAveragePrice() + " REAL AVG: " + getAvg(abr));
System.out.println("amount: " + getGallons(abr));
//abr.getPurchases().forEach(p -> System.out.println(p.getTotalPrice()));
System.out.println("TOTAL is: " + abr.getPurchases().stream().mapToDouble(Purchase::getTotalPrice).sum());
}

Expand Down
13 changes: 9 additions & 4 deletions src/knapsack/GreedyBoundedKnapsackSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ private Purchase decidePurchase(int needGallons, List<Commodity> commodities, bo

private Purchase bestForCommodity(Commodity cmdty, int needGallons, boolean min) {
int canTake = min(cmdty.getAmountLeft(), needGallons);
int canTakeByServings = getCanTakeByServings(cmdty, canTake, min);
if (canTakeByServings == 0) return null;

cmdty.decreaseAmount(canTakeByServings);
return new Purchase(cmdty.getSource(), canTakeByServings, cmdty.getAvgPrice());
}

private int getCanTakeByServings(Commodity cmdty, int canTake, boolean min) {
int canTakeByServings = (canTake / cmdty.getServingSize()) * cmdty.getServingSize();

if ((min && canTake < cmdty.getMinSize())) {
canTakeByServings = cmdty.getAmountLeft()>=cmdty.getMinSize() ? cmdty.getMinSize() : canTakeByServings;
}
if (canTakeByServings == 0) return null;

cmdty.decreaseAmount(canTakeByServings);
return new Purchase(cmdty.getSource(), canTakeByServings, cmdty.getAvgPrice());
return canTakeByServings;
}
}

0 comments on commit 4901dc0

Please sign in to comment.