LC 683. K Empty Slots 【lock,hard】
There 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: 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:
- 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】的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 解题报告-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 ...
- [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 ...
- LC 425. Word Squares 【lock,hard】
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- linux tty终端个 pts伪终端 telnetd伪终端
转:http://blog.sina.com.cn/s/blog_735da7ae0102v2p7.html 终端tty.虚拟控制台.FrameBuffer的切换过程详解 Framebuffer Dr ...
- 05-【session、cookie】
session.cookie 1.HttpSession概述>HttpSession是由JavaWeb提供的,用来会话跟踪的类.session是服务器端对象,保存在服务器端!!!>Http ...
- (七)make menuconfig
1.make menuconfig进入图形界面后,输入 / 进行查找页面,如果输入有错,要删除前面输入的可以输入 ctrl加<--键(ctrl加回退按键)
- 转载 如何使用批处理 动态改变path实现改变JDK版本
http://www.cnblogs.com/xdp-gacl/p/5209386.html 1 @echo off 2 3 rem --- Base Config 配置JDK的安装目录 --- 4 ...
- Linux工具之ss
1.SS命令 (Socket Statistics),获取socket统计信息,显示和netstat类似的内容.显示更详细的TCP连接信息. 命令功能: ss(Socket Statistic ...
- angular 中同级元素交替样式
事件 : ng-click="addNews()" 所属div的层级: div > div >span 即,对于 event.target 查找的话最多 从s ...
- java8学习之Lambda表达式初步与函数式接口
对于Java8其实相比之前的的版本增加的内容是相当多的,其中有相当一大块的内容是关于Lambda表达式与Stream API,而这两部分是紧密结合而不能将其拆开来对待的,但是是可以单独使用的,所以从学 ...
- PAT乙级1024
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805297229447168 题解 第一遍也是没有全部AC,有3 ...
- sql 实现取表中相同id时间最大的一行 利用distinct on
数据表是这样的 select * from water_level_records m where ( select count(*) from water_level_records n where ...
- LeetCode01 - 两数之和(Java 实现)
LeetCode01 - 两数之和(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum 题目描述 给定一个整数数组 ...