Coilility NailingPlanks

问题描述 投票:-2回答:1

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

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

您将获得两个由N个整数组成的非空数组A和B。这些阵列代表N个木板。更准确地说,A [K]是起点,B [K]第K个木板的末端。

接下来,您将获得一个由M个整数组成的非空数组C。这个阵列代表M钉。更准确地说,C [I]是您可以锤击第I颗钉子。

我们说如果有钉子C [I],就钉一块木板(A [K],B [K])使得A [K]≤C [I]≤B [K]。

目标是找到必须使用的最少指甲数量直到钉好所有木板为止。换句话说,您应该找到一个值J使得仅使用第一个后​​所有木板都将被钉牢J钉子更确切地说,对于每个木板(A [K],B [K]),使得0≤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没有钉住当前木板,该怎么办?

解决方案是否有误?

java algorithm binary-search codility
1个回答
0
投票

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

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

由于我们只对不同木板所需的最少索引中的最大索引(即“最差”木板的索引)感兴趣,因此,我们知道刚刚找到的索引不再重要。我们可以退出循环并返回一个不会影响结果的“虚拟”索引。

请注意调用循环中的效果:

result = getMinIndex(A[i], B[i], sortedNail, result); 

[分配完成后,result将等于通话前的result:可以用较早的钉子钉在此木板(A[i], B[i])上,但是我们并不需要真正关心哪个钉子,因为我们需要知道最坏的情况,由result反映出来。

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

© www.soinside.com 2019 - 2024. All rights reserved.