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. deep_learning_Function_np.newaxis参数理解

    功能:np.newaxis是用来给数组a增加维度的格式:a[np.newaxis和:的组合],如a[:,np.newaxis],a[np.newaxis, np.newaxis, :]详解:np.ne ...

  2. DNS负载均衡与NGINX负载均衡策略

    负载均衡是指的是把请求均匀的分摊到多个服务器上处理.一般常见的负载均衡有两种:①客户端与反向代理服务器之间的DNS负载均衡②反向代理服务器与应用服务器之间的负载均衡(这种负载均衡有很多,可以是webl ...

  3. java 如何实现文件变动的监听

    获取修改时间 long lastTime = file.lastModified(); 原文链接:https://blog.csdn.net/liuyueyi25/article/details/79 ...

  4. 使用OmniDiskSweeper清理MAC

    Mac 经常提示我磁盘空间已满,管理磁盘空间. 然后我就管理了一下,发现系统竟占90个G,有点懵逼.然后网上查了资料 使用了一个名叫OmniDiskSweeper的超级强大的工具,而且还是免费的,它能 ...

  5. github高速下载的方法

    windows修改host文件: C:\Windows\System32\drivers\etc\hostslinux 修改host文件: /etc/hosts 在文件后面加上这两行 151.101. ...

  6. 四 java web考点

    一.GET和POST区别(参考Servlet&JSP学习笔记) <form>中method属性默认为GET. 1.使用POST的情况 GET跟随URL之后,请求参数长度有限,过长的 ...

  7. 【51nod 1667】概率好题

    题目 甲乙进行比赛. 他们各有k1,k2个集合[Li,Ri] 每次随机从他们拥有的每个集合中都取出一个数 S1=sigma甲取出的数,S2同理 若S1>S2甲胜 若S1=S2平局 否则乙胜 分别 ...

  8. 【JZOJ5605】【NOI2018模拟3.26】Arg

    题目描述 给出一个长度为 m 的序列 A, 请你求出有多少种 1...n 的排列, 满足 A 是它的一个 LIS. 解题思路 如何求出一个序列的LIS? 对于二分的方法,每次插入一个数,将它放到第一个 ...

  9. 有效的结构化思维训练,MECE分析法

    MECE原则,表达精准分类与全面性的有效利器 结构化思维的本质就是逻辑,其目的在于对问题的思考更完整.更有条理,它帮助我们一个一个找到线头,理清思路,探求事物之间的相互联系.MECE分析法是一种结构化 ...

  10. HDU - 3586 Information Disturbing 树形dp二分答案

    HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...