Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.IntStream;
  7.  
  8. public class Main {
  9.  
  10. public static int solution(int[] A) {
  11. int arrLength = A.length;
  12.  
  13. Set<Integer> vacationsSet = IntStream.of(A).boxed().collect(Collectors.toSet());
  14. int locationsCount = vacationsSet.size();
  15. Set<Integer>[] visitedLocations = new Set<Integer>[arrLength - locationsCount + 1];
  16. Integer[] shortestVacation = new Integer[arrLength - locationsCount + 1];
  17. int currentShortestVacation = arrLength;
  18. int startDay = 0;
  19.  
  20. for (int day = 0; day < arrLength; day++) {
  21. int location = A[day];
  22.  
  23. for(int prevDay = Math.min(0, day - currentShortestVacation); prevDay <= day; ++prevDay) {
  24. Set<Integer> locations = visitedLocations[prevDay];
  25. if (null == locations) {
  26. locations = new HashSet<Integer>(locationsCount];
  27. visitedLocations[prevDay] = locations
  28. }
  29. locations.add(location);
  30. if (locations.size() == locationsCount && day - prevDay < currentShortestVacation) {
  31. currentShortestVacation = day - prevDay;
  32. startDay = prevDay
  33. }
  34. }
  35. }
  36.  
  37. return currentShortestVacation;
  38. }
  39.  
  40. public static void main(String[] args) {
  41. System.out.println(solution(new int[]{2, 1, 1, 3, 2, 1, 1, 3}));
  42. System.out.println(solution(new int[]{7, 5, 2, 7, 2, 7, 4, 7}));
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement