Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class Main {
- public static int solution(int[] A) {
- int arrLength = A.length;
- Set<Integer> vacationsSet = IntStream.of(A).boxed().collect(Collectors.toSet());
- int shortestVacation = arrLength;
- for (int day = 0; day < arrLength; day++) {
- int tempShortestVacation = 0;
- int startCounter = day;
- Set<Integer> remainingLocs = new HashSet<>(vacationsSet);
- while (!remainingLocs.isEmpty()) {
- int location = A[startCounter];
- tempShortestVacation++;
- startCounter++;
- if (startCounter >= arrLength) {
- return shortestVacation;
- }
- remainingLocs.remove(location);
- }
- if (tempShortestVacation < shortestVacation) {
- shortestVacation = tempShortestVacation;
- }
- if (shortestVacation == vacationsSet.size()) {
- return shortestVacation;
- }
- }
- return shortestVacation;
- }
- public static void main(String[] args) {
- System.out.println(solution(new int[]{2, 1, 1, 3, 2, 1, 1, 3}));
- System.out.println(solution(new int[]{7, 5, 2, 7, 2, 7, 4, 7}));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement