Minimum Consecutive Cards to Pick Up LeetCode solution in Java

1
class Solution {
2
public int minimumCardPickup(int[] cards) {
3
Map<Integer, Integer> map = new HashMap<>();
4
int min = Integer.MAX_VALUE;
5
for (int i = 0; i < cards.length; i++) {
6
if (map.containsKey(cards[i]))
7
min =
8
Math.min(
9
i - map.get(cards[i]) + 1,
10
min); // Check if the difference in indices is smaller than minimum
11
map.put(cards[i], i); // Update the last found index of the card
12
}
13
return min == Integer.MAX_VALUE ? -1 : min; // Repetition found or not
14
}
15
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0