Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 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.  
  15.  
  16.         int shortestVacation = arrLength;
  17.  
  18.         for (int day = 0; day < arrLength; day++) {
  19.             int tempShortestVacation = 0;
  20.             int startCounter = day;
  21.             Set<Integer> remainingLocs = new HashSet<>(vacationsSet);
  22.  
  23.             while (!remainingLocs.isEmpty()) {
  24.                 int location = A[startCounter];
  25.                 tempShortestVacation++;
  26.                 startCounter++;
  27.                 if (startCounter >= arrLength) {
  28.                     return shortestVacation;
  29.                 }
  30.  
  31.                 remainingLocs.remove(location);
  32.             }
  33.  
  34.             if (tempShortestVacation < shortestVacation) {
  35.                 shortestVacation = tempShortestVacation;
  36.             }
  37.  
  38.             if (shortestVacation == vacationsSet.size()) {
  39.                 return shortestVacation;
  40.             }
  41.         }
  42.  
  43.         return shortestVacation;
  44.     }
  45.  
  46.     public static void main(String[] args) {
  47.         System.out.println(solution(new int[]{2, 1, 1, 3, 2, 1, 1, 3}));
  48.         System.out.println(solution(new int[]{7, 5, 2, 7, 2, 7, 4, 7}));
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement