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;
}
};

LC 683. K Empty Slots 【lock,hard】的更多相关文章

  1. LC 727. Minimum Window Subsequence 【lock,hard】

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  2. LC 465. Optimal Account Balancing 【lock,hard】

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  3. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  4. 解题报告-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 ...

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

  6. LC 425. Word Squares 【lock,hard】

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  7. LC 774. Minimize Max Distance to Gas Station 【lock,hard】

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  8. LC 644. Maximum Average Subarray II 【lock,hard】

    Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...

  9. LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】

    Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...

随机推荐

  1. 关于同一台服务器上两个PHP项目相互访问超时的问题

    当一台服务器部署多个PHP项目,各自运行时并无干扰, 即使都使用 9000端口来跑php 但是有一种情况,当其中一个项目需要调用另一个php项目的接口时,便会超时,这是因为php是单线程的同步的 也许 ...

  2. js数组的所有方法

    修改器方法 下面的这些方法会改变调用它们的对象自身的值: Array.prototype.copyWithin()  在数组内部,将一段元素序列拷贝到另一段元素序列上,覆盖原有的值. Array.pr ...

  3. linux命令详解——crontab

    基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...

  4. Delphi MSComm 控件方法

  5. ubuntu系统搭建samba服务

    安装samba服务 # apt-get install samba 创建一个samba服务的分享目录 # mkdir /share 创建一个samba服务限制的用户及组 #useradd public ...

  6. deep_learning_Function_tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法

    [Tensorflow] tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法 作用:输出正确的预测结果利用tf.argmax()按行求出真实值y_.预测值y最大值 ...

  7. 卸载TensorFlow

    卸载TensorFlow 1.先用pip3 list查看安装了那些TensorFlow,一般只有两个,另一个是TensorBoard 2.执行命令卸载 sudo apt remove --purge ...

  8. 两步创建vue全局组件

    import Login from './Login' export default { install: function(Vue){ Vue.component('Login', Login); ...

  9. 将本地代码使用Git上传更新至Github

    注册.配置git 1. 首先注册git image 2.然后下载.配置git 百度“git下载”,然后默认安装,注意的是最后要添加环境变量,最后安装结果如下: image 配置如下: 1.设置本地的s ...

  10. 清北学堂dp图论营游记day5

    ysq主讲: tarjan缩点+拓扑+dij最短路. floyd..... 单源..最长路... 建正反两个图. 二分答案,把大于答案的边加入到新图中,如果能走过去到终点,就可以. 或者:从大到小加边 ...