Untitled
import java.util.*; public class Solution { public int minimumCardPickup(int[] cards) { Set<Integer> seen = new HashSet<>(); int minLength = Integer.MAX_VALUE; int left = 0; for (int right = 0; right < cards.length; right++) { int card = cards[right]; // If the card is already in the set, calculate the window size while (seen.contains(card)) { minLength = Math.min(minLength, right - left + 1); seen.remove(cards[left]); left++; } // Add the current card to the set seen.add(card); } return minLength == Integer.MAX_VALUE ? -1 : minLength; } }
Leave a Comment