LWC 51:683. K Empty Slots

LWC 51:683. K Empty Slots

传送门:683. K Empty Slots

Problem:

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].

题意:

题目要求满足开花的两个slot之间的间隙恰好为k个的天数。

很暴力,遍历每个位置i,因为在位置i的左侧一定都是开花的,而在位置i的右侧则都是还没开过的花。所以在左侧的位置中,找到两个相邻位置之差为k个即可,为了快速比较相邻slot之间的间隙,i在不断累加的同时,始终保持i左侧位置的有序性,在插入的同时,去比较它的左侧位置,和右侧位置,看是否符合间隙之差为k+1。

当然上述操作需要维护位置的有序性,于是采用了插入排序算法,并且输出插入对应的位置。这样就能方便的找寻当前位置的左侧位置和右侧位置。

代码如下:

    public int kEmptySlots(int[] flowers, int k) {
        int n = flowers.length;
        if (n == 1 && k == 0) return 1;
        sort = new int[n + 16];
        for (int i = 0; i < n; ++i) {
            int index = add(flowers[i]);
            int min = index - 1 < 0 ? -11111111 : sort[index - 1];
            int max = index + 1 >= tot ? -11111111 : sort[index + 1];
            if (valid(flowers[i], k, min, max)) return i + 1;
        }
        return -1;
    }

    boolean valid(int x, int k, int min, int max) {
        if (max - x == k + 1) return true;
        if (x - min == k + 1) return true;
        return false;
    }

    int[] sort;
    int tot = 0;
    public int add(int x) {
        int j = 0;
        while (j < tot && sort[j] < x) {
            ++j;
        }
        for (int i = tot - 1; i >= j; --i) {
            sort[i + 1] = sort[i];
        }
        sort[j] = x;
        tot++;
        return j;
    }

当然,你也可以使用JAVA自带的数据结构,TreeSet来实现,代码精简很多。

代码如下:

    public int kEmptySlots(int[] flowers, int k) {
        int n = flowers.length;
        if (n == 1 && k == 0) return 1;
        TreeSet<Integer> sort = new TreeSet<>();
        for (int i = 0; i < n; ++i) {
            sort.add(flowers[i]);
            Integer min = sort.lower(flowers[i]);
            Integer max = sort.higher(flowers[i]);
            int mi = min == null ? -1111111 : min;
            int ma = max == null ? -1111111 : max;
            if (valid(flowers[i], k, mi, ma)) return i + 1;
        }
        return -1;
    }

    boolean valid(int x, int k, int min, int max) {
        if (max - x == k + 1) return true;
        if (x - min == k + 1) return true;
        return false;
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1182
来自专栏码匠的流水账

java9系列(五)Stack-Walking API

java9新增这个类的目的是提供一个标准API用于访问当前线程栈,之前只有Throwable::getStackTrace、Thread::getStackTr...

471
来自专栏Pulsar-V

Save Camera Document

#pragma once #include "HCCamera.h" #include <time.h> #include <cstdio> #incl...

2888
来自专栏码匠的流水账

聊聊spring cloud的HystrixAutoConfiguration

本文主要研究一下spring cloud的HystrixAutoConfiguration

1102
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

6306
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

1162
来自专栏MelonTeam专栏

Bitmap 源码阅读笔记

导语: Android 系统上的图片的处理,跟Bitmap 这个类脱不了关系,我们有必要去深入阅读里面的源码,以便在工作中能更好的处理Bitmap相关的问题...

2598
来自专栏我和未来有约会

简练的视图模型 ViewModel

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silver...

2319
来自专栏linux驱动个人学习

高通msm8909耳机调试

1、DTS相应修改: DTS相关代码:kernel/arch/arm/boot/dts/qcom/msm8909-qrd-skuc.dtsi: 1 s...

7885
来自专栏linux驱动个人学习

高通Audio中ASOC的machine驱动

ASoC被分为Machine、Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的...

1K4

扫码关注云+社区