题目:

一个花园有N个位置。每个位置上有一朵花。这N朵花会在N天内逐一盛开。每天都一定会有并且只有一朵花盛开,从这天起,这朵花将一直处于盛开的状态。

给定一个由数字1N组成的数组flowers。数组中的每个数字表示那一天将会盛开的花的位置。

例如,flowers[i] = x表示在位置x上的花会在第i天盛开,其中ix都在1N的范围内。

还有,给出一个整数k,你需要返回,在哪一天,恰好有两朵花处于盛开的状态,并且两朵花之间恰好有k朵花没有盛开。

如果有多个答案,选择最小的。
如果不存在这样一天,那么返回-1。

  • 给定数组在[1, 20000]范围内。

样例 1:

输入:[1,2,3,4],k=1
输出:-1
解释:最先开放的两朵花相邻

样例 2:

输入:[1,3,2],k=1
输出:2
解释:在第二天,第一朵和第三朵花会开

分析:

给定一个开花序列,每天只开一朵花,求两朵花处于盛开时且他们之间恰好有k朵花没有盛开。

第一种思路就是,每开一朵花时,就相应的去检查它前k+1朵花和后k+1朵是否盛开,且他们之间没有花开,如果存在的话返回相应的天数即可。

第二种我们可以用一个有序集合存储每一个开花位置,每新加入到集合中,就检查前一个位置和后一个开花的位置是否符合要求。

第三种我们不存储所有开花位置,而是把开花的位置以K+1为单位分成若干和区间,每个区间只记录当前的最大位置和最小位置。同一个区间内的开花位置是不符合间隔k这个要求,每开一朵花,更新相应区间的最大位置和最小位置,如果是最大位置,那么仅有可能在下一个区间的最大位置符合当前要求,检查即可,同理,如果是最小位置,则仅可能在上一个区间的最小位置符合当前要求。

程序:

C++

class Solution {
public:
/**
* @param flowers: the place where the flower will open in that day
* @param k: an integer
* @return: in which day meet the requirements
*/
int kEmptySlots(vector<int> &flowers, int k) {
// Write your code here
int n = flowers.size();
if(k >= n || n == 0)
return -1;
set<int> flo;
for(int i = 0; i < n; ++i){
int x = flowers[i];
flo.insert(x);
auto l = flo.find(x);
auto r = l;
if(++r != flo.end() && *r == x + k + 1){
return i + 1;
}
if(l != flo.begin() && *(--l) == x - k - 1){
return i + 1;
}
}
return -1;
}
};
class Solution {
public:
/**
* @param flowers: the place where the flower will open in that day
* @param k: an integer
* @return: in which day meet the requirements
*/
int kEmptySlots(vector<int> &flowers, int k) {
// Write your code here
int n = flowers.size();
if (n == 0 || k >= n) return -1;
int bs = (n - 1) / (k + 1) + 1;
vector<int> higher(bs, INT_MIN);
vector<int> lower(bs, INT_MAX);
for(int i = 0; i < n; ++i){
int x = flowers[i];
int p = (x - 1) / (k + 1);
if (x < lower[p]) {
lower[p] = x;
if (p > 0 && higher[p - 1] == x - k - 1) return i + 1;
}
if (x > higher[p]) {
higher[p] = x;
if (p < bs - 1 && lower[p + 1] == x + k + 1) return i + 1;
}
}
return -1;
}
};

Java

public class Solution {
/**
* @param flowers: the place where the flower will open in that day
* @param k: an integer
* @return: in which day meet the requirements
*/
public int kEmptySlots(int[] flowers, int k) {
// Write your code here
int n = flowers.length;
if(k >= n || n == 0)
return -1;
TreeSet<Integer> flo = new TreeSet();
for(int i = 0; i < n; ++i){
int x = flowers[i];
flo.add(x);
Integer low = flo.lower(x);
Integer high = flo.higher(x);
if(low != null && low == x - k - 1)
return i+1;
if(high != null && high == x + k + 1)
return i+1;
}
return -1;
}
}
public class Solution {
/**
* @param flowers: the place where the flower will open in that day
* @param k: an integer
* @return: in which day meet the requirements
*/
public int kEmptySlots(int[] flowers, int k) {
// Write your code here
int n = flowers.length;
if (n == 0 || k >= n) return -1;
int bs = (n - 1) / (k + 1) + 1;
int[] higher = new int[bs];
int[] lower = new int[bs];
Arrays.fill(higher, Integer.MIN_VALUE);
Arrays.fill(lower, Integer.MAX_VALUE);
for(int i = 0; i < n; ++i){
int x = flowers[i];
int p = (x - 1) / (k + 1);
if (x < lower[p]) {
lower[p] = x;
if (p > 0 && higher[p - 1] == x - k - 1) return i + 1;
}
if (x > higher[p]) {
higher[p] = x;
if (p < bs - 1 && lower[p + 1] == x + k + 1) return i + 1;
}
}
return -1;
}
}

LeetCode 683. K Empty Slots K 个空花盆 / LintCode 861. K个空的位置 (C++/Java)的更多相关文章

  1. LC 683. K Empty Slots 【lock,hard】

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  2. 解题报告-683. K Empty Slots

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  3. [LeetCode] 683. K Empty Slots K个空槽

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  4. [LeetCode] K Empty Slots K个空槽

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  5. [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  6. 【LeetCode】1461. 检查一个字符串是否包含所有长度为 K 的二进制子串 Check If a String Contains All Binary Codes of Size K

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计长度为 K 的子串个数 日期 题目地址:https ...

  7. [Swift]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  8. [Swift]LeetCode992. K 个不同整数的子数组 | Subarrays with K Different Integers

    Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A g ...

  9. [Swift]LeetCode1015. 可被 K 整除的最小整数 | Smallest Integer Divisible by K

    Given a positive integer K, you need find the smallest positive integer N such that N is divisible b ...

  10. hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙

    /** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...

随机推荐

  1. Llama 3 开源了「GitHub 热点速览」

    近日,Meta(原 Facebook)开源了他们公司的新一代大模型 Llama 3,虽然目前只放出了 8B 和 70B 两个版本,但是在评估结果上已经优于 Claude 3 Sonnet.Mistra ...

  2. HarmonyOS NEXT应用开发—验证码布局

    介绍 本示例介绍如何使用Text组件实现验证码场景,并禁用对内容的选中.复制.光标. 效果图预览 使用说明 单击组件可弹出输入法 在进行验证码输入时,无法对中间单个数字进行更改,无法选中输入内容,无光 ...

  3. 解析 RocketMQ 业务消息——“事务消息”

    简介: 本篇文章通过拆解 RocketMQ 事务消息的使用场景.基本原理.实现细节和实战使用,帮助大家更好的理解和使用 RocketMQ 的事务消息. 作者:合伯   引言:在分布式系统调用场景中存在 ...

  4. 最佳实践丨构建云上私有池(虚拟IDC)的5种方案详解

    ​简介:云上私有池系列终篇终于来了,本文将重点介绍构建云上的私有池(虚拟IDC)的多种方案和各自的优缺点,并给出相关的性价比优化建议. 本文作者:阿里云技术专家李雨前 摘要 围绕私有池(虚拟IDC)的 ...

  5. 聚焦 | 数据湖分析如何面向对象存储OSS进行优化?

    简介: 最佳实践,以DLA为例子.DLA致力于帮助客户构建低成本.简单易用.弹性的数据平台,比传统Hadoop至少节约50%的成本.其中DLA Meta支持云上15+种数据数据源(OSS.HDFS.D ...

  6. dotnet 6 已知问题 获取 CultureInfo.NumberFormat 可能抛出 IndexOutOfRangeException 异常

    本文记录一个 dotnet 6 已知问题,准确来说这是一个在 dotnet 5 引入的问题,到 dotnet 6.0.12 还没修.在获取 CultureInfo.NumberFormat 属性时,在 ...

  7. 国内常用源开发环境换源(flutter换源,python换源,Linux换源,npm换源)

    国内开源镜像站点 大学 清华大学开源软件镜像站 (使用较多) 中国科学技术大学开源软件镜像 (使用较多) 浙江大学开源镜像站 (主要是各发行版Linux,pip等常用库) 哈尔滨工业大学开源镜像站 ( ...

  8. ChatGPT 眼中的程序员

    1 你如何看待程序员这个行业 程序员这个行业在现代社会扮演着非常重要的角色.作为一个技术驱动的职业,程序员们负责开发.设计和维护软件和应用程序,这些技术改变了我们的生活方式.商业模式和社会互动方式. ...

  9. VGA显示文字

    VGA显示文字 VGA字符显示的原理 把要显示的字符转换成字符点阵,然后编码存储,着色的部分为1,其它为0.然后在VGA上输出显示. 字符点阵生成软件: https://www.zhetao.com/ ...

  10. 羽夏闲谈—— Kdenlive flatpak 版本解决语音识别找不到 pip3

    简述   Kdenlive是在 Linux 上一款比较优秀的剪辑软件,功能比较强大.操作相对容易,能够满足日常剪辑的需要. 解决方案   找到对应的安装目录/var/lib/flatpak/app/o ...