-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
66 lines (61 loc) · 2.06 KB
/
Copy pathsolution.cpp
File metadata and controls
66 lines (61 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool isPossible(vector<int> &arr, int k)
{
// If array is not guaranteed sorted, sort it.
// GeeksforGeeks usually gives sorted array, but sorting is safe.
sort(arr.begin(), arr.end());
// comparator: first by value, then by length (both ascending)
auto comp = [](const pair<int, int> &a, const pair<int, int> &b)
{
if (a.first == b.first)
return a.second > b.second; // smaller length has higher priority => return true when a > b
return a.first > b.first; // smaller number has higher priority
};
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> pq(comp);
int i = 0, n = arr.size();
while (i < n)
{
if (pq.empty())
{
pq.push({arr[i], 1});
++i;
}
else
{
auto top = pq.top();
if (arr[i] == top.first)
{
// same value as end of some subsequences -> start a new subsequence
pq.push({arr[i], 1});
++i;
}
else if (arr[i] == top.first + 1)
{
// extend the shortest subsequence that ends with top.first
pq.pop();
pq.push({arr[i], top.second + 1});
++i;
}
else
{
// arr[i] > top.first + 1 : top subsequence can no longer be extended
if (top.second < k)
return false;
pq.pop();
}
}
}
// finally ensure every subsequence has length >= k
while (!pq.empty())
{
if (pq.top().second < k)
return false;
pq.pop();
}
return true;
}
};