fork download
  1. /*
  2. solution by Abdrashim Eldan
  3. contact: eldan.abdrashim@gmail.com
  4. */
  5. // struct cmp{
  6. // bool operator()(int i, int j){
  7. // return i > j;
  8. // }
  9. // };
  10.  
  11. class Solution {
  12. public:
  13. int longestConsecutive(vector<int>& a) {
  14. // priority_queue <int, vector <int>, int> pq it's increase sorted PQ
  15. priority_queue <int> pq;
  16. int n = a.size();
  17. for(int i = 0; i < n; i++){
  18. pq.push(a[i]);
  19. }
  20. int maxLen = 1;
  21. while(!pq.empty()){
  22. int num = pq.top();
  23. pq.pop();
  24. int len = 1;
  25. while(!pq.empty() && num - 1 == pq.top()){
  26. len++;
  27. num = pq.top();
  28. pq.pop();
  29. while(!pq.empty() && num == pq.top()){
  30. pq.pop(); //erase same numbers
  31. }
  32. }
  33. maxLen = max(maxLen, len);
  34. }
  35. return maxLen;
  36. }
  37. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:13:28: error: 'vector' has not been declared
     int longestConsecutive(vector<int>& a) {
                            ^
prog.cpp:13:34: error: expected ',' or '...' before '<' token
     int longestConsecutive(vector<int>& a) {
                                  ^
prog.cpp: In member function 'int Solution::longestConsecutive(int)':
prog.cpp:15:9: error: 'priority_queue' was not declared in this scope
         priority_queue <int> pq;
         ^
prog.cpp:15:25: error: expected primary-expression before 'int'
         priority_queue <int> pq;
                         ^
prog.cpp:16:17: error: 'a' was not declared in this scope
         int n = a.size();
                 ^
prog.cpp:18:13: error: 'pq' was not declared in this scope
             pq.push(a[i]);
             ^
prog.cpp:21:16: error: 'pq' was not declared in this scope
         while(!pq.empty()){
                ^
prog.cpp:33:37: error: 'max' was not declared in this scope
             maxLen = max(maxLen, len);
                                     ^
stdout
Standard output is empty