列表

详情


2274. 不含特殊楼层的最大连续楼层数

Alice 管理着一家公司,并租用大楼的部分楼层作为办公空间。Alice 决定将一些楼层作为 特殊楼层 ,仅用于放松。

给你两个整数 bottomtop ,表示 Alice 租用了从 bottomtop(含 bottomtop 在内)的所有楼层。另给你一个整数数组 special ,其中 special[i] 表示  Alice 指定用于放松的特殊楼层。

返回不含特殊楼层的 最大 连续楼层数。

 

示例 1:

输入:bottom = 2, top = 9, special = [4,6]
输出:3
解释:下面列出的是不含特殊楼层的连续楼层范围:
- (2, 3) ,楼层数为 2 。
- (5, 5) ,楼层数为 1 。
- (7, 9) ,楼层数为 3 。
因此,返回最大连续楼层数 3 。

示例 2:

输入:bottom = 6, top = 8, special = [7,6,8]
输出:0
解释:每层楼都被规划为特殊楼层,所以返回 0 。

 

提示

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int maxConsecutive(int bottom, int top, vector<int>& special) { } };

rust 解法, 执行用时: 11 ms, 内存消耗: 4.1 MB, 提交时间: 2025-01-06 23:25:55

impl Solution {
    pub fn max_consecutive(bottom: i32, top: i32, mut special: Vec<i32>) -> i32 {
        special.push(bottom - 1);
        special.push(top + 1);
        special.sort_unstable();
        special.windows(2).map(|w| w[1] - w[0] - 1).max().unwrap()
    }
}

rust 解法, 执行用时: 14 ms, 内存消耗: 3.9 MB, 提交时间: 2025-01-06 23:25:39

impl Solution {
    pub fn max_consecutive(bottom: i32, top: i32, mut special: Vec<i32>) -> i32 {
        special.sort_unstable();
        let n = special.len();
        let mut ans = (special[0] - bottom).max(top - special[n - 1]);
        for i in 1..n {
            ans = ans.max(special[i] - special[i - 1] - 1);
        }
        ans
    }
}

javascript 解法, 执行用时: 129 ms, 内存消耗: 62.6 MB, 提交时间: 2025-01-06 23:25:18

/**
 * @param {number} bottom
 * @param {number} top
 * @param {number[]} special
 * @return {number}
 */
var maxConsecutive = function(bottom, top, special) {
    special.sort((a, b) => a - b);
    let n = special.length;
    let ans = Math.max(special[0] - bottom, top - special[n - 1]);
    for (let i = 1; i < n; i++) {
        ans = Math.max(ans, special[i] - special[i - 1] - 1);
    }
    return ans;
};

java 解法, 执行用时: 34 ms, 内存消耗: 54.6 MB, 提交时间: 2025-01-06 23:24:55

class Solution {
    public int maxConsecutive(int bottom, int top, int[] special) {
        Arrays.sort(special);
        int n = special.length;
        int ans = Math.max(special[0] - bottom, top - special[n - 1]);
        for (int i = 1; i < n; i++) {
            ans = Math.max(ans, special[i] - special[i - 1] - 1);
        }
        return ans;
    }
}

golang 解法, 执行用时: 152 ms, 内存消耗: 9.7 MB, 提交时间: 2022-12-11 12:41:56

func maxConsecutive(bottom, top int, a []int) (ans int) {
	a = append(a, bottom-1, top+1)
	sort.Ints(a)
	for i := 1; i < len(a); i++ {
		ans = max(ans, a[i]-a[i-1]-1)
	}
	return
}

func max(a, b int) int { if b > a { return b }; return a }

python3 解法, 执行用时: 164 ms, 内存消耗: 25.7 MB, 提交时间: 2022-12-11 12:41:33

class Solution:
    def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
        special.append(bottom - 1)
        special.append(top + 1)
        special.sort()
        return max(y - x - 1 for x, y in pairwise(special))

cpp 解法, 执行用时: 136 ms, 内存消耗: 56.7 MB, 提交时间: 2022-12-11 12:40:46

class Solution {
public:
    int maxConsecutive(int bottom, int top, vector<int>& special) {
        special.push_back(bottom - 1);
        special.push_back(top + 1);
        sort(special.begin(), special.end());

        int n = special.size();
        int ans = 0;
        for (int i = 0; i < n - 1; ++i) {
            ans = max(ans, special[i + 1] - special[i] - 1);
        }
        return ans;
    }
};

python3 解法, 执行用时: 220 ms, 内存消耗: 26.2 MB, 提交时间: 2022-12-11 12:40:22

class Solution:
    def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
        special.sort()
        ans = max(special[0] - bottom, top - special[-1])
        n = len(special)
        for i in range(0, n - 1):
            if special[i+1] - special[i] - 1 > ans:
                ans = special[i+1] - special[i] - 1
        
        return ans

上一题