leetcode293周赛6064. 不含特殊楼层的最大连续楼层数

leetcode293周赛6064. 不含特殊楼层的最大连续楼层数,第1张

一:题目

二:上码
// 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;
    }
};

自己写的超时了 赛后 膜拜大佬的 还是太菜了

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/921854.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存