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. Java反射【三、方法的反射】

    获取一个类下的所有方法 可以获取类类型后,获取到所有方法及相关信息 Method[] ms = c.getMethods(); 获取方法列表(public) Method[] ms = c.getDe ...

  2. springboot搭建web项目与使用配置文件

    目录 一.准备工作 二.创建基础web项目 1. maven配置 2.创建maven项目.配置pom.xml为web基础项目 3.编写启动类 4.使用maven打包 5.使用命令java -jar x ...

  3. 内置函数 lambda sorted filter map 递归

    一 lambda 匿名函数 为了解决一些简单的需求而设计的一句话函数 # 计算 n 的 n次方 def func(n): return n**n print(func(10)) f = lambda ...

  4. 3.NumPy - 数组属性

    1.ndarray.shape 这一数组属性返回一个包含数组维度的元组,它也可以用于调整数组大小 # -*- coding: utf-8 -*- import numpy as np a = np.a ...

  5. API开发之接口安全(一)----生成sign

    在对于API的开发中 最让人头疼的 就是接口数据暴露 让一些有心之人 抓包之后恶意请求 那么如何解决这一弊端呢?自然而然的 我们就想到了 加密  那我们又如何加密 如何解密 才能使之有最安全的效率呢? ...

  6. Summer training #4

    D:找到两个数 一个是另一个的整数倍(1也算) 因为N是600000 调和级数为ln(n+1) 算一下 可以直接爆 #include <bits/stdc++.h> #include &l ...

  7. Bridge 桥梁模式

    注:桥梁模式是结构型设计模式,将抽象部分与它的实现部分相分离,使他们可以独立的变化.  抽象部分可能有n种实现,而实现部分可能有n种实现方式,采用享元模式,减少子类数据. 曾经看过一个桥梁模式相关的例 ...

  8. Solr——Java应用

    Solr有一个客户端SolrJ 创建一个Java Project 引入Jar包 添加test类 package com.solr.test; import java.io.IOException; i ...

  9. 返回vector指针

    #include<iostream> #include <vector> using namespace std; vector<int> *MyFind() { ...

  10. Spring Boot 中application.yml与bootstrap.yml的区别(转)

    yml与properties其实yml和properties文件是一样的原理,且一个项目上要么yml或者properties,二选一的存在. 推荐使用yml,更简洁. bootstrap与applic ...