博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
683. K Empty Slots
阅读量:5845 次
发布时间:2019-06-18

本文共 1447 字,大约阅读时间需要 4 分钟。

here 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: 1Output: 2Explanation: In the second day, the first and the third flower have become blooming.

 

Example 2:

Input: flowers: [1,2,3]k: 1Output: -1

 

Note:

class Solution {public:    int kEmptySlots(vector
& flowers, int k) { set
bloom; for (int i = 0; i < flowers.size(); i++) { int p = flowers[i]; auto it = bloom.insert(p).first; if (it != bloom.begin()) { if (p-*(--it) == k+1) return i+1; it++; } if (++it != bloom.end() && *it-p == k+1) return i+1; } return -1; }};

 

转载于:https://www.cnblogs.com/jxr041100/p/7885853.html

你可能感兴趣的文章
除了模拟手术教学,VR在医疗领域如何应用?
查看>>
JVM性能调优之如何书写高效优雅的代码
查看>>
谈数据中心“容灾和备份的区别”
查看>>
linux的LVM
查看>>
不同的类UNIX操作系统密码破解方法介绍
查看>>
MySQL 5.6 for Windows配置安装之解压缩版
查看>>
ubuntu jdk
查看>>
hive报错(2)udf无法找到第三方的类
查看>>
HashCode
查看>>
Nginx解析PHP问题
查看>>
A2SD 命令集
查看>>
实例:Linux EXT3文件系统下成功恢复误删的文件[原创]
查看>>
盘点5款Ubuntu监控工具解决CPU暴增问题
查看>>
linux_定时任务
查看>>
Xorg <= 1.10 remote root 0day exploit (32-bit x86)
查看>>
2010下半年网络规划设计师上午试卷、标准参考答案及分析(1)
查看>>
szshunjia储存不干胶标签的心得简述分享
查看>>
最近几年做软件项目的心得总结
查看>>
10个网页设计师必备的CSS技巧(转)
查看>>
我的友情链接
查看>>