java - Codility 钉板

标签 java algorithm binary-search

试图了解 Codility NailingPlanks 的解决方案。

问题链接:
https://app.codility.com/programmers/lessons/14-binary_search_algorithm/nailing_planks/

You are given two non-empty arrays A and B consisting of N integers. These arrays represent N planks. More precisely, A[K] is the start and B[K] the end of the K−th plank.

Next, you are given a non-empty array C consisting of M integers. This array represents M nails. More precisely, C[I] is the position where you can hammer in the I−th nail.

We say that a plank (A[K], B[K]) is nailed if there exists a nail C[I] such that A[K] ≤ C[I] ≤ B[K].

The goal is to find the minimum number of nails that must be used until all the planks are nailed. In other words, you should find a value J such that all planks will be nailed after using only the first J nails. More precisely, for every plank (A[K], B[K]) such that 0 ≤ K < N, there should exist a nail C[I] such that I < J and A[K] ≤ C[I] ≤ B[K].



解决方法链接:
https://github.com/ZRonchy/Codility/blob/master/Lesson12/NailingPlanks.java
import java.util.Arrays;

class Solution {
    public int solution(int[] A, int[] B, int[] C) {
        // the main algorithm is that getting the minimal index of nails which
        // is needed to nail every plank by using the binary search
        int N = A.length;
        int M = C.length;
        // two dimension array to save the original index of array C
        int[][] sortedNail = new int[M][2];
        for (int i = 0; i < M; i++) {
            sortedNail[i][0] = C[i];
            sortedNail[i][1] = i;
        }
        // use the lambda expression to sort two dimension array
        Arrays.sort(sortedNail, (int x[], int y[]) -> x[0] - y[0]);
        int result = 0;
        for (int i = 0; i < N; i++) {//find the earlist position that can nail each plank, and the max value for all planks is the result
            result = getMinIndex(A[i], B[i], sortedNail, result);
            if (result == -1)
                return -1;
        }
        return result + 1;
    }
    // for each plank , we can use binary search to get the minimal index of the
    // sorted array of nails, and we should check the candidate nails to get the
    // minimal index of the original array of nails.
    public int getMinIndex(int startPlank, int endPlank, int[][] nail, int preIndex) {
        int min = 0;
        int max = nail.length - 1;
        int minIndex = -1;
        while (min <= max) {
            int mid = (min + max) / 2;
            if (nail[mid][0] < startPlank)
                min = mid + 1;
            else if (nail[mid][0] > endPlank)
                max = mid - 1;
            else {
                max = mid - 1;
                minIndex = mid;
            }
        }
        if (minIndex == -1)
            return -1;
        int minIndexOrigin = nail[minIndex][1];
        //find the smallest original position of nail that can nail the plank
        for (int i = minIndex; i < nail.length; i++) {
            if (nail[i][0] > endPlank)
                break;
            minIndexOrigin = Math.min(minIndexOrigin, nail[i][1]);
            // we need the maximal index of nails to nail every plank, so the
            // smaller index can be omitted    ****
            if (minIndexOrigin <= preIndex) // ****
                return preIndex;            // ****
        } 
        return minIndexOrigin;
    }
}

我不明白第 99-102 行,标有 **** , 的解决方案。

我的问题是:

minIndexOrigin <= preIndex ,那么它将使用 preIndex ,
但是如果preIndex不钉当前的木板?

解决方案有什么错误吗?

最佳答案

在这些行中处理的情况是,当您发现存在钉住当前木板的指数时,该指数小于(或等于)我们需要能够钉住另一个(先前分析过的)木板的最低指数。在这种情况下,我们不需要进一步寻找当前的木板,因为我们知道:

  • 我们可以钉木板
  • 我们可以使用一个不大于我们真正需要用于另一个木板的索引的索引。

  • 由于我们只对不同板条所需的最少索引中的最大索引感兴趣(即“最差”板条的索引),我们知道我们刚刚找到的索引不再重要:如果我们已经知道我们将至少使用所有钉子 preIndex ,我们知道该组中的一个钉子将钉住当前的木板。我们可以退出循环并返回一个不会影响结果的“虚拟”索引。

    注意调用循环中的效果:
    result = getMinIndex(A[i], B[i], sortedNail, result); 
    

    完成此任务后,result将等于什么 result之前打电话:这个木板(A[i], B[i])可以用更早的钉子钉,但我们并不真正关心那是哪颗钉子,因为我们需要知道最坏的情况,到目前为止,这是由result反射(reflect)的。 ,并且所有达到该索引的钉子都已经在将被锤击的钉子组中。

    您可以将其与 alpha-beta 剪枝进行比较。

    关于java - Codility 钉板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62133735/

    相关文章:

    java - 使用 vlcj 从文件夹流式传输多个文件

    java - 删除 Maven 程序集中 jar 的所有依赖项

    algorithm - 您可以使用什么算法来查找字符串中的重复短语?

    c++ - 涉及 vector 的二进制搜索有问题 (C++)

    algorithm - 实现二分搜索有哪些陷阱?

    Java - 包括 BST

    java - 如何跟踪 Android 中 RecyclerView 中的选定项目?

    python - 给定一个树高列表,找到尽可能少的尝试将它们砍倒

    algorithm - 计算图的顶点连通性

    python - 使用递归的二分搜索进入无限循环