import java.util.ArrayList; import java.util.List; public class TRAII_24_X6_skeleton implements TRAII_24_X6 { // ^^^ own id here /** * SELF-EVALUATION HERE: * * * */ /** * Distribute list of input elements so that there will be as many as possible such lists that have at least required * minSum as the sum of elements. * * @param input elements to distribute * @param boxSize minimum content for each box * @param maxTime maximum time to use in seconds * @return list of result lists (boxes) */ @Override public List> fruitPacking(ArrayList input, int boxSize, int maxTime) { List> result = new ArrayList<>(); // check for too small input if (sum(input) < boxSize) { throw new RuntimeException("Input smaller than box size, cannot create valid result."); } // create first box List openBox = new ArrayList<>(); result.add(openBox); // all boxes in the result int openBoxWeight = 0; // process the input for (int elem : input) { // add the element to current open box openBox.add(elem); openBoxWeight += elem; // if box in now full enough, take a new box if (openBoxWeight >= boxSize) { openBox = new ArrayList<>(); result.add(openBox); openBoxWeight = 0; } } // remove the last incomplete box by adding its elements to the second last box if (result.size() > 1 && sum(result.get(result.size() - 1)) < boxSize) { result.get(result.size() - 2).addAll(result.get(result.size() - 1)); result.remove(result.size() - 1); } return result; } int sum(List box) { return box.stream().reduce(0, Integer::sum); } }