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 locationsCount = vacationsSet.size();
- Set<Integer>[] visitedLocations = new Set<Integer>[arrLength - locationsCount + 1];
- Integer[] shortestVacation = new Integer[arrLength - locationsCount + 1];
- int currentShortestVacation = arrLength;
- int startDay = 0;
- for (int day = 0; day < arrLength; day++) {
- int location = A[day];
- for(int prevDay = Math.min(0, day - currentShortestVacation); prevDay <= day; ++prevDay) {
- Set<Integer> locations = visitedLocations[prevDay];
- if (null == locations) {
- locations = new HashSet<Integer>(locationsCount];
- visitedLocations[prevDay] = locations
- }
- locations.add(location);
- if (locations.size() == locationsCount && day - prevDay < currentShortestVacation) {
- currentShortestVacation = day - prevDay;
- startDay = prevDay
- }
- }
- }
- return currentShortestVacation;
- }
- 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