返回介绍

solution / 2200-2299 / 2274.Maximum Consecutive Floors Without Special Floors / README_EN

发布于 2024-06-17 01:03:08 字数 3287 浏览 0 评论 0 收藏 0

2274. Maximum Consecutive Floors Without Special Floors

中文文档

Description

Alice manages a company and has rented some floors of a building as office space. Alice has decided some of these floors should be special floors, used for relaxation only.

You are given two integers bottom and top, which denote that Alice has rented all the floors from bottom to top (inclusive). You are also given the integer array special, where special[i] denotes a special floor that Alice has designated for relaxation.

Return _the maximum number of consecutive floors without a special floor_.

 

Example 1:

Input: bottom = 2, top = 9, special = [4,6]
Output: 3
Explanation: The following are the ranges (inclusive) of consecutive floors without a special floor:
- (2, 3) with a total amount of 2 floors.
- (5, 5) with a total amount of 1 floor.
- (7, 9) with a total amount of 3 floors.
Therefore, we return the maximum number which is 3 floors.

Example 2:

Input: bottom = 6, top = 8, special = [7,6,8]
Output: 0
Explanation: Every floor rented is a special floor, so we return 0.

 

Constraints:

  • 1 <= special.length <= 105
  • 1 <= bottom <= special[i] <= top <= 109
  • All the values of special are unique.

Solutions

Solution 1

class Solution:
  def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
    special.sort()
    ans = max(special[0] - bottom, top - special[-1])
    for i in range(1, len(special)):
      ans = max(ans, special[i] - special[i - 1] - 1)
    return ans
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;
  }
}
function maxConsecutive(bottom: number, top: number, special: number[]): number {
  let nums = special.slice().sort((a, b) => a - b);
  nums.unshift(bottom - 1);
  nums.push(top + 1);
  let ans = 0;
  const n = nums.length;
  for (let i = 1; i < n; i++) {
    ans = Math.max(ans, nums[i] - nums[i - 1] - 1);
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文