LeetCode 0683
来源:互联网 发布:斯诺登现状2017 知乎 编辑:程序博客网 时间:2024/06/01 10:50
683. K Empty Slots
原题链接
我的思路:
首先,题目用的变量有点奇怪,注意这点:
flowers[i] = xmeans that the unique flower that blooms at day
iwill be at position
x
其次,题目中有这么一句话:
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.
这是说,对于给定的k,你要找到这么一天,使得存在两朵已经开的花,并且这两朵花之间有k朵花,并且这k朵花都是没有开放的。
那么一种思路就是,遍历每一天,使得一朵花开放之后,是否存在连续k朵不开放的花。假设有n朵已经开放的花
我从网上找了一份代码,利用的是set的有序性:
class Solution {public: int kEmptySlots(vector<int>& flowers, int k) { set<int> bloom; for (int i = 0; i < flowers.size(); i++) { int p = flowers[i]; bloom.insert(p); auto it = bloom.find(p); 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; }};
阅读全文
0 0
- LeetCode 0683
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- 使用struts2框架遇到的问题
- jsp+servlet验证码验证
- Leetcode 53 Maximum Subarray
- Maximum Subarray
- Leetcode 566 Reshape the Matrix
- LeetCode 0683
- install phantomjs 2.1.1
- “程序媛计划”--女孩们的魔法棒
- Evaluate Division
- Leetcode 70 Climbing Stairs
- linux fb /lcd 驱动
- JAVA中的反射机制
- 数据库事务4种隔离级别详解
- 3624: [Apio2008]免费道路