There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will be in the status of blooming since then.

Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day.

For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x, where i and x will be in the range from 1 to N.

Also given an integer k, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is k and these flowers are not blooming.

If there isn't such day, output -1.

Example 1:
Input:
flowers: [1,3,2]
k: 1
Output: 2
Explanation: In the second day, the first and the third flower have become blooming.
Example 2:
Input:
flowers: [1,2,3]
k: 1
Output: -1
Note:
The given array will be in the range [1, 20000].

解法一: 时间O(N * LogN) 空间 O(N)

思路: 对于任意一个位置i, 假设i开花了, i 需要看下左侧离它最近的开花位置是不是i - k - 1, 或者看下右侧离它最近的位置是不是i + k + 1,

所以是个搜索问题. 搜索的话,我们知道有上下界查找,在java中,查数A的上界即找第一个大于该数的值,下界即找最后一个小于该数的值, 前提是整个数有序

而在本题中,我们可以使用treeset保存开花位置,每输入一个新的位置,在TreeSet中找左侧离该位置最近的位置,或者右侧离该位置最近的位置,即查找上下界

判断距离上下界的距离是否为k,第一个满足条件的为结果

class Solution {
public int kEmptySlots(int[] flowers, int k) {
TreeSet<Integer> tree = new TreeSet<>();
int day = 0;
for (int flower : flowers) {
++day;
tree.add(flower);
Integer lower = tree.lower(flower);
if (lower != null && flower - lower - 1 == k) return day; Integer higher = tree.higher(flower);
if (higher != null && higher - flower - 1 == k) return day;
} return -1;
}
}

解法二: 时间O(N) 空间O(N)

思路: 维护每个位置开花的日期, 如果存在某个区间符合条件,那么需要满足a[left] < min(a[left + 1] ... a[left + k]) && a[right] < min(a[left + 1] ... a[left + k])

可以考虑维护这个区间内的最小值,用两端的值跟区间内最小值比较,难点是该如何维护这个数据结构?

a[left] 要么比区间内的最小值大,要么等于,要么小于

因为我们要维护区间内的最小值,区间内的淘汰规则应该是小的淘汰大的,

所以如果a[left]比区间内的最小值小,那么假设我们维护了最小值队列,那么a[left]应该在区间前面

如果a[left]比区间内最小值大,那么它不会出现在最小值队列中

假设我们维护两个队列,一个存原始数据, 一个存区间内的最小值

假设left = 0, 第一个区间的right 应该为 k + 1

在入队第k个元素后,我们构造了前k个数的最小队列, a[left] 可能在队列中,也可能不在

我们先从原始队列中弹出a[left], 然后如果a[left] == 最小值队列的头部元素值,则从最小值队列中也把该值弹出

然后比较 a[left] 与 最小值队列当前的队头值,然后比较a[right]和最小值队头值, 如果满足条件a[left] < min(a[left + 1] ... a[left + k]) && a[right] < min(a[left + 1] ... a[left + k])

则得到一个可能的结果,最终从这些结果中,挑值最小的即为最终解

class Solution {
public int kEmptySlots(int[] flowers, int k) {
int[] days = new int[flowers.length];
for (int i = 0; i < days.length; i++) {
days[flowers[i] - 1] = i + 1;
} int ans = Integer.MAX_VALUE;
MinQueue<Integer> mins = new MinQueue<>();
for (int i = 0; i < days.length; i++) {
mins.addLast(days[i]);
if (i >= k) {
Integer x = mins.pollFirst();
if (mins.isEmpty()) {
if (i + 1 < days.length) {
ans = Math.min(ans, Math.max(x, days[i + 1]));
}
} else {
if (x < mins.min() && i + 1 < days.length && days[i + 1] < mins.min()) {
ans = Math.min(ans, Math.max(x, days[i + 1]));
}
}
}
} if (ans == Integer.MAX_VALUE) {
ans = -1;
} return ans;
}
} class MinQueue<E extends Comparable<E>> extends ArrayDeque<E> {
Deque<E> mins; public MinQueue() {
mins = new ArrayDeque<E>();
} @Override
public void addLast(E x) {
super.addLast(x);
while (mins.peekLast() != null && x.compareTo(mins.peekLast()) < 0) {
mins.pollLast();
}
mins.addLast(x);
} @Override
public E pollFirst() {
E x = super.pollFirst();
if (0 == x.compareTo(mins.peekFirst())) {
mins.pollFirst();
}
return x;
} public E min() {
return mins.peekFirst();
}
}

解法三: 时间O(N) 空间O(N)

思路: 与解法二思路相同, 维护K + 2大小的区间, 假设有两个满足条件的区间

[k1, k2] 和 [k3, k4], 可以得到一个结论: 这两个区间不能重合

因为假设两区间重合 k1 < k3 < k2 < k4, 那么k3 和 k4 中间存在一个比它俩都小的值,则[k3, k4]不满足解的条件,与已知条件矛盾

所以不可能重合.

同时对于一个区间内,如果遇到一个位置p, p < k1 或者 p < k2, 那么p之前位置都不可能是解, 此时,我们从p开始重新查找可行解.

class Solution {
public int kEmptySlots(int[] flowers, int k) {
int[] days = new int[flowers.length];
for (int i = 0; i < days.length; i++) {
days[flowers[i] - 1] = i + 1;
} int left = 0, right = k + 1;
int ans = Integer.MAX_VALUE;
search:
while (right < days.length) {
for (int j = left + 1; j < right; j++) {
if (days[j] < days[left] || days[j] < days[right]) {
left = j;
right = j + k + 1;
continue search;
}
} ans = Math.min(ans, Math.max(days[left], days[right]));
left = right;
right = left + k + 1;
} if (ans == Integer.MAX_VALUE) ans = -1;
return ans;
}
}

解题报告-683. K Empty Slots的更多相关文章

  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. [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 ...

  3. [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 ...

  4. 【九度OJ】题目1174:查找第K小数 解题报告

    [九度OJ]题目1174:查找第K小数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1174 题目描述: 查找一个数组的第 ...

  5. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  6. 【剑指Offer】链表中倒数第k个节点 解题报告(Python)

    [剑指Offer]链表中倒数第k个节点 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-intervie ...

  7. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  8. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  9. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

随机推荐

  1. 40道Java基础常见面试题及详细答案

    最近看到网上流传着各种面试经验及面试题,往往都是一大堆技术题目贴上去,但是没有答案. 为此我业余时间整理了40道Java基础常见的面试题及详细答案,望各路大牛发现不对的地方不吝赐教,留言即可. 八种基 ...

  2. ubuntu修改分辨率

    ubuntu分辨率真是坑爹,不知什么原因,ubuntu分辨率一下子变了,通过,显示->分辨率,都改不过来... 无奈,有重装系统的冲动,从网上找了很多方法,都不管用,有一次,在这里 sudo g ...

  3. JAVA使用ItextPDF

    1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等.方便用户查看,下载,打印.目前常用的解决方案是,把相关数据信息,生成对应的pdf文件返回给用户. ...

  4. .NET/C# 使用反射注册事件

    使用反射,我们可以很容易地在运行时调用一些编译时无法确定的属性.方法等.那么如何注册事件呢? 本文将介绍如何使用反射注册事件. 本文内容 不使用反射 使用反射 安全地使用反射 参考资料 不使用反射 例 ...

  5. [TopCoder12141]SweetFruits

    vjudge description 有\(n\)个水果,有一些水果是香的,它们各有一个香度值,剩下的水果是不香的. 现在你要把这\(n\)个水果连成一棵树,定义一个水果是真香的当且仅当它是香的且他与 ...

  6. 洛谷P4197 Peaks(Kruskal重构树 主席树)

    题意 题目链接 往后中文题就不翻译了qwq Sol 又是码农题..出题人这是强行把Kruskal重构树和主席树拼一块了啊.. 首先由于给出的限制条件是<=x,因此我们在最小生成树上走一定是最优的 ...

  7. 查看ms SQL Server存储过程,函数的内容

    方法1:最简单的,右键单击要查看的存储过程,选择“修改”: 方法2: SELECT definition FROM solar.sys.sql_modules WHERE [object_id]=(O ...

  8. WinForm中Application.Idle事件用法

    Application.Idle 事件 描述:当应用程序完成处理并即将进入空闲状态时发生.如果您有必须执行的任务在线程变为空闲之前,请将它们附加到此事件. public partial class F ...

  9. 马士兵Spring-声明式事务管理-annotation

    1.事务加在DAO层还是service层? service中可能多涉及多种DAO的操作,比如存了一个User之后,需要保存一条日志信息:如果在DAO中分别设置事务的话,一个DAO下面方法抛出异常了,但 ...

  10. POJ 2395 Out of Hay(求最小生成树的最长边+kruskal)

    Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18472   Accepted: 7318 Descr ...