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 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:
- 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 ...
随机推荐
- Ceph自动化部署----Ceph-ansible
目录 Ceph自动化部署----Ceph-ansible 一.前言--Ceph的几种不同的部署方式 二.使用Ceph-ansible部署Ceph Ceph自动化部署----Ceph-ansible 一 ...
- Hadoop_09_HDFS 的 NameNode工作机制
理解NameNode的工作机制尤其是元数据管理机制,以增强对HDFS工作原理的理解,及培养hadoop集群运营中“性能调优” “NameNode”故障问题的分析解决能力 1.NameNode职责: H ...
- Android异常与性能优化相关面试问题-ui卡顿面试问题详解
UI卡顿原理: “60fps(Frames Per Second每秒传输帧数) ----> 16ms” 针对上面标红的数字,下面具体说明一下:最主要的根源在于渲染性,Android会每隔16ms ...
- usb发送字节
- 清除LabVIEW中波形图表或波形图中的历史数据
清除LabVIEW中波形图表或波形图中的历史数据 方法一: 前面板中右键单击波形图表或波形图,选择数据操作>>清除图表或数据操作>>清除图形 方法二:(编程方法) 用于清除图表 ...
- 报错:required string parameter XXX is not present
报错:required string parameter XXX is not present 不同工具发起的get/delete请求,大多数不支持@RequestParam,只支持@PathVari ...
- 设置Portainer管理Docker并且开启https(简单方法)
1. 序言 Portainer是一个十分好用的docker图形化管理界面,可以很方便的查看容器状态,错误log等等. 2. 安装 安装portainer是十分简单的,只需要执行docker pull ...
- 一步一步理解线段树——转载自JustDoIT
一步一步理解线段树 目录 一.概述 二.从一个例子理解线段树 创建线段树 线段树区间查询 单节点更新 区间更新 三.线段树实战 -------------------------- 一 概述 线段 ...
- python播放音乐
最近一直想实现使用Python播放音乐的功能,找了百度上的好多博客,要不就只能播放wav格式的,要不播放mp3格式的但无法在Linux系统下使用的,或者只能在Python2的情况下播放的,写的都不符合 ...
- vue中单选框与多选框的实现与美化
我们在做一些页面时,可能会用到很多的单选框和复选框,但是原生的radio和checkbox前面的原型图标或方框样式不尽人意.于是,决定自己来实现单选框和复选框.我用的是vue,所以就用vue的方式实现 ...