Skip to content

Commit 85ac94d

Browse files
authored
Create minimum-consecutive-cards-to-pick-up.cpp
1 parent 9703c65 commit 85ac94d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// hash table
5+
class Solution {
6+
public:
7+
int minimumCardPickup(vector<int>& cards) {
8+
static const int INF = numeric_limits<int>::max();
9+
unordered_map<int, int> lookup;
10+
int result = INF;
11+
for (int i = 0; i < size(cards); ++i) {
12+
if (lookup.count(cards[i])) {
13+
result = min(result, i - lookup[cards[i]] + 1);
14+
}
15+
lookup[cards[i]] = i;
16+
}
17+
return result != INF ? result : -1;
18+
}
19+
};

0 commit comments

Comments
 (0)