There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in Ndays. 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:

  1. The given array will be in the range [1, 20000].

Runtime: 128 ms, faster than 89.38% of C++ online submissions for K Empty Slots.

网上的解法。

class Solution {
public:
int kEmptySlots(vector<int>& flowers, int k) {
int n = flowers.size();
vector<int> days(n, );
for (int i = ; i < flowers.size(); i++) {
days[flowers[i] - ] = i + ;
}
int left = , right = k+, ret = INT_MAX;
for (int i = ; right < n; i++) {
if (days[i] < days[left] || days[i] <= days[right]) {
if (i == right) ret = min(ret, max(days[left], days[right]));
left = i;
right = i + k + ;
}
}
return ret == INT_MAX ? - : ret;
}
};

最新文章

  1. CSS初始化样式
  2. Base:-用数组赋值实现while和shift功能
  3. PHP 数据安全问题总结
  4. 前端手札--meta标记篇
  5. SDWebImage笔记
  6. 如何理解和熟练使用JS 中的call apply
  7. Redis初步
  8. 配置struts tags 输出HTML
  9. CSV文件规则
  10. 《JavaScript高级程序设计 第3版》-学习笔记-1
  11. scala学习笔记——特质
  12. hdu 4467 Graph
  13. 用 label 控制 Pod 的位置 - 每天5分钟玩转 Docker 容器技术(128)
  14. java三大版本和核心优势
  15. Nginx 负载均衡一致性算法
  16. python的高阶函数式编程
  17. 微信小程序之wx.request:fail错误,真机预览请求无效问题解决,安卓,ios网络预览异常
  18. UVA 12293 - Box Game(博弈)
  19. 前端开发---HTML---介绍
  20. Windows下查看端口常用命令以及关闭端口的方法

热门文章

  1. Tomcat设置默认启动项目
  2. Java 访问 C++ 方法:JavaCPP
  3. 获取iframe子页面内容高度给iframe动态设置高度
  4. 【转】通过lombok带你读透Builder构建器
  5. mysql数据库:数据类型、存储引擎、约束、
  6. while 循环 及 and or not
  7. 1.Lucene简介
  8. js去掉url后某参数【函数封装】
  9. MHA原理
  10. kotlin函数和函数式表达式