LeetCode 683. K Empty Slots K 个空花盆 / LintCode 861. K个空的位置 (C++/Java)
题目:
一个花园有N个位置。每个位置上有一朵花。这N朵花会在N天内逐一盛开。每天都一定会有并且只有一朵花盛开,从这天起,这朵花将一直处于盛开的状态。
给定一个由数字1到N组成的数组flowers。数组中的每个数字表示那一天将会盛开的花的位置。
例如,flowers[i] = x表示在位置x上的花会在第i天盛开,其中i和x都在1到N的范围内。
还有,给出一个整数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)的更多相关文章
- 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 ...
- 解题报告-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 ...
- [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 ...
- [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 ...
- [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 ...
- 【LeetCode】1461. 检查一个字符串是否包含所有长度为 K 的二进制子串 Check If a String Contains All Binary Codes of Size K
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计长度为 K 的子串个数 日期 题目地址:https ...
- [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 ...
- [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 ...
- [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 ...
- hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙
/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...
随机推荐
- Java面试题:请谈谈对ThreadLocal的理解?
ThreadLocal是一种特殊的变量存储机制,它提供了一种方式,可以在每个线程中保存数据,而不会受到其他线程的影响.这种机制在多线程编程中非常有用,因为它允许每个线程拥有自己的数据副本,从而避免了数 ...
- 关于mac使用figma以及企业微信3.1.18版本,CPU系统占用率飙升至70%
问题描述 使用一段时间正常,不知道是修改了什么设置,还是有什么软件冲突,导致cpu使用率极高 系统进程中有一个 kernel_task 占用了大量的cpu 并且该占用并不会消失,只要figma打开就会 ...
- 力扣591(java)-标签验证器(困难)
题目: 给定一个表示代码片段的字符串,你需要实现一个验证器来解析这段代码,并返回它是否合法.合法的代码片段需要遵守以下的所有规则: 代码必须被合法的闭合标签包围.否则,代码是无效的. 闭合标签(不一定 ...
- 基于 K8s 的交付难题退退退!| 独家交付秘籍(第三回)
简介: 经过仔细研究,我们发现秘籍中提到许多帮助解决交付问题的招式,而其中一个让我们印象很深,是关于在原有社区版容器底座 Kubernetes(以下简称 K8s)的基础上,对容器底座进行改进,可更好的 ...
- RocketMQ 5.0: 存储计算分离新思路
简介: 在阿里云上,RocketMQ 的商业化产品也以弹性云服务的形式为全球数万个用户提供企业级的消息解决方案,被广泛应用于互联网.大数据.移动互联网.物联网等领域的业务场景,成为了业务开发的首选消息 ...
- PolarDB-X 发布 2.1.0 版本,Paxos 重磅开源
简介:2022年4月1号,PolarDB-X 正式开源X-Paxos,基于原生MySQL存储节点,提供Paxos三副本共识协议,可以做到金融级数据库的高可用和容灾能力,做到RPO=0的生产级别可用性 ...
- 国产 Web 组态软件 TopStack V5.0 发布
简介 TopStack 是一款轻量型 Web 组态软件,提供设备数据采集.定时任务.控制策略.联动控制.设备告警.设备维护管理.设备绩效管理.能源管理.组态开发.报表开发等核心功能.支持移动端访问,支 ...
- Debian(WSL)安装gprMax教程 - 适用于Windows系统
原文发布于:https://blog.zhaoxuan.site/archives/33.html: 第一时间获取最新文章请关注博客个人站:https://blog.zhaoxuan.site. 1. ...
- windows版 navicat_15.0.18 安装
视频安装地址: https://www.ghpym.com/ghvideo07.html 一.下载安装包 下载地址(百度网盘): 链接:https://pan.baidu.com/s/1MIZfmS5 ...
- 关于armcc中static __inline修饰符的记录
相关的知识点:内联函数,static关键字 在一次stm32的库代码分析中发现static __inline 的函数定义,对此有些疑惑,static和inline两个关键字为什么要进行连用呢? 对此进 ...