Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cpecmu.cpe218.sp2019.hw2.submit;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.*;
- import cpecmu.cpe218.sp2019.Pair;
- import cpecmu.cpe218.sp2019.hw2.CommanderInspection;
- public class CommanderInspectionImpl implements CommanderInspection {
- /**
- * input file name
- */
- protected static final String inFile = "hw2tests/cmdinspect02.in";
- /*
- Might be useful:
- If you need to sort a list l, you can use method
- Collections.sort(l, comp)
- where comp is a Comparator, taking two elements from the list,
- and returning a negative integer if the first is "less than" the second,
- a positive integer if the first is "greater than" the second,
- and zero if they are equal.
- Example for comparing two pairs of integers:
- Comparator<Pair<Integer, Integer>> comp = (p1, p2) -> {
- // Compare first component first.
- // If not equal, the lower element is less.
- if (p1.fst() != p2.fst()) return p1.fst() - p2.fst();
- // If the first components are equal,
- // compare the second element.
- return p1.snd() - p2.snd();
- };
- See Java API on java.util.Comparator interface for more details.
- */
- @Override
- public Set<Integer> inspectionTimes(List<Pair<Integer, Integer>> shifts) {
- // TODO Your code here
- List<Pair<Integer, Integer>> availableList = shifts;
- List<Pair<Integer, Integer>> temp = new ArrayList<>();
- List<Pair<Integer, Integer>> acceptedList = new ArrayList<>();
- Comparator<Pair<Integer, Integer>> comp1 = (p1, p2) -> {
- if (p1.fst() != p2.fst()) return p1.fst() - p2.fst();
- return p1.snd() - p2.snd();
- };
- Collections.sort(availableList, comp1);
- while (!availableList.isEmpty()) {
- acceptedList.add(availableList.get(availableList.size() - 1));
- availableList.remove(availableList.size() - 1);
- Collections.sort(acceptedList, comp1);
- temp = new ArrayList<>();
- for (int i = 0; i < availableList.size(); i++) {
- if (availableList.get(i).snd() < acceptedList.get(0).fst()) {
- temp.add(availableList.get(i));
- }
- }
- availableList = temp;
- }
- Set<Integer> set = new LinkedHashSet<>();
- for (int i = 0; i < acceptedList.size(); i++)
- set.add(acceptedList.get(i).fst());
- return set;
- }
- public static void main(String[] args) {
- try (FileReader fr = new FileReader(inFile);
- Scanner s = new Scanner(fr)) {
- // read number of shifts
- int n = s.nextInt();
- List<Pair<Integer, Integer>> shifts = new ArrayList<>(n);
- for (int i = 0; i < n; i++) {
- // read shift
- shifts.add(new Pair<>(s.nextInt(), s.nextInt()));
- }
- // invoke algorithm
- CommanderInspection sbm = new CommanderInspectionImpl();
- Set<Integer> res = sbm.inspectionTimes(shifts);
- System.out.println("We need to do the minimum of " + res.size() + " inspection.");
- System.out.print("One possible way is to inspect at time ");
- System.out.println(res);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement