// class Solution {
// public:
// bool find(vector& v,int i) {
// for (auto nums:v) {
// if (nums == i) return true;//包含某个数 就返回true
// }
// return false;
// }
// int maxConsecutive(int bottom, int top, vector& special) {
// int maxx = 0;
// int count = 0;
// for (int i = bottom; i <= top; i++) {
// if (!find(special,i)) {//不包含某个数
// count++;
// }else {
// maxx = max(maxx,count);
// count = 0;
// }
// }
// maxx = max(maxx,count);
// return maxx;
// }
// };
// class Solution {
// public:
// bool find(vector& v,int i) {
// for (auto nums:v) {
// if (nums == i) return true;//包含某个数 就返回true
// }
// return false;
// }
// int maxConsecutive(int bottom, int top, vector& special) {
// int maxx = 0;
// int i = bottom;
// special.push_back(top+1);
// for (int j = bottom; j <= top+1; j++) {
// if (find(special,j)) {//包含某个数
// if (j-i > maxx) maxx = j-i;
// i = j+1;
// }
// }
// // maxx = max(maxx,count);
// return maxx;
// }
// };
class Solution {
public:
/**
思路:
首先将序列进行排序
1.第一部分是 specila[0] - bottom
2.第二部分是我们在特殊楼层之间的间隙 special[i] - spcial[i-1] - 1 eg 4 6-->6 - 4 -1 = 1 (这个1就表示的是5层)
3.第三部分就是 top - special[special.size()-1]
*/
int maxConsecutive(int bottom, int top, vector<int>& special) {
sort(special.begin(),special.end());
int ans = 0;
ans = max(ans,special[0] - bottom);
for (int i = 1; i < special.size(); i++) {
ans = max(ans,special[i]-special[i-1]-1);
}
ans = max(ans,top-special[special.size()-1]);
return ans;
}
};
自己写的超时了 赛后 膜拜大佬的 还是太菜了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)