CareerCup Find the biggest interval that has all its members in list in O(n)
来源:互联网 发布:菜单设计制作软件 编辑:程序博客网 时间:2024/06/07 01:47
Given a list of integers, find out the biggest interval that has all its members in the given list. e.g. given list 1, 7, 4, 6, 3, 10, 2 then answer would be [1, 4]. Develop algorithm and write code for this
-------------------------------------------------------------------------
What is biggest interval? It means the longest consecutive integers. Take the above example, 1, 2, 3, 4 are members of the list.
#include<iostream>#include<stdio.h>#include<stack>#include<vector>#include<string>#include<unordered_set>#include<unordered_map>#include<set>#include<queue>#include<map>using namespace std;class Solution { public: void largestRange(vector<int>& lst, vector<int>& range) { unordered_set<int> table; range[0] = 0; range[1] = -1; int size = lst.size(), i, j, cur, a, b; for (i = 0; i < size; ++i) table.insert(lst[i]); for (unordered_set<int>::iterator it = table.begin(); table.size() > 0; it = table.begin()) { cur = *it; table.erase(it); for (i = cur + 1; table.size() > 0; ++i) { if (table.find(i) == table.end()) break; table.erase(i); } b = i - 1; for (j = cur - 1; table.size() > 0; --j) { if (table.find(j) == table.end()) break; table.erase(j); } a = j + 1; if (b - a > range[1] - range[0]) { range[1] = b; range[0] = a; } } }};int main() { Solution s; int a[] = {1, 7, 4, 6, 3, 10, 2}; vector<int> v(a, a + 7); vector<int> range(2); s.largestRange(v, range); cout<<range[0]<<" "<<range[1]<<endl; return 0;}
1 0
- CareerCup Find the biggest interval that has all its members in list in O(n)
- CareerCup Finds all the elements that appear more than n/3 times in the list
- CareerCup Fill the array with product of all numbers except the number in that cell
- CareerCup Find all the conflicting appointments from a given list of n appointments.
- List all in the Directory
- Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
- CareerCup Find the usernames existing in both documents
- Find All Duplicates in an Array 算法复杂度O(n)的C++实现
- List all the position that two kings not encounter in Chinese chess
- How to get all the members in user group by using LDAP in Perl?
- CareerCup Write a function that given a position returns the digit in that 0123456789101112131415..
- The proof is in the code. That is all.
- In a bunch of number to find out the biggest minimum value
- The 'Apple Developer Program License Agreement' has been updated. In order to access certain members
- The 'Apple Developer Program License Agreement' has been updated. In order to access certain members
- Find the elements in the set that appears only once
- find the N-th node In Inorder
- CareerCup Output the amount of all possible strings of length N that don't of have consecutive a,b,c
- 如何在android.mk中使用shell命令
- apache上传下载
- U盘安装centos
- libevent源码分析--epoll_dispatch()
- android LayerDrawable 及layer-list
- CareerCup Find the biggest interval that has all its members in list in O(n)
- 一个新基因的产生
- NBU 巡检报错:You are already running availabe_media.try again later
- 网页中播放flv视频文件
- How To Quickly Add/Remove Column Statistics (Histograms) For A Column (文档 ID 390249.1
- 赚积分的
- System.load 和 System.loadLibrary详解
- Linux线程私有数据pthread_key_t
- 无法定位程序输入点***于动态链接库QtCore4.dll的解决方法