leetcode 題號1122 Relative Sort Array

查看題目詳情可點擊此處

題目

Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.

Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don’t appear in arr2 should be placed at the end of arr1 in ascending order.

Example 1:

Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]

Constraints:

  • arr1.length, arr2.length <= 1000
  • 0 <= arr1[i], arr2[i] <= 1000
  • Each arr2[i] is in arr1.
  • Each arr2[i] is distinct.

解題思路

根據題目可知,被排序的數組 arr1 會被分爲兩塊以不同的排序方式進行排序再組合,完成整個數組的排序。排序完成的數組前半部分是根據關聯子數組 arr2 中的元素順序排序,arr2 的元素在 arr1 都有對應數據,arr1 中會有重複數據存在的情況,arr2 中的數據都是唯一的,arr1 與 arr2 中對應不上的元素根據從小至大的順序排列。

我是先記錄 arr2 中元素分別在 arr1 中出現的次數,並且將這些元素都交換至 arr1 的靠前部分,交換結束後 arr1 的靠後部分就是與 arr2 中元素對應不上的元素,我們可以對這部分元素按從小到大排序,此時記錄的 arr2 元素分別在 arr1 中出現的次數就派上用場,根據元素在 arr2 中的順序以及在 arr1 中出現的次數(其實就是個數)在 arr1 的前部分進行填充,最後 arr1 就是題目中需要的結果。

代碼實現

代碼中使用 relativeSort 方法進行題目要求的排序。

public class RelativeSortArray {
    public static int[] relativeSort(int[] array, int[] relative) {
        Map<Integer, Integer> relativeMap = generateRelativeMap(relative);
        int relativeIdx = filterRelativeArea(array, relativeMap);
        notRelativeSort(array, relativeIdx);
        fillRelativeArray(array, relative, relativeMap);
        return array;
    }

    public static Map<Integer, Integer> generateRelativeMap(int[] relative) {
        Map<Integer, Integer> relativeMap = new HashMap<>();
        for (int i = 0; i < relative.length; i++) {
            relativeMap.put(relative[i], 0);
        }
        return relativeMap;
    }

    public static boolean existRelative(int num, Map<Integer, Integer> relativeMap) {
        return relativeMap.containsKey(num);
    }

    public static int filterRelativeArea(int[] array, Map<Integer, Integer> relativeMap) {
        int relativeIdx = 0;
        for (int i = 0; i < array.length; i++) {
            if (existRelative(array[i], relativeMap)) {
                Integer count = relativeMap.get(array[i]);
                relativeMap.put(array[i], ++count);
                int temp = array[relativeIdx];
                array[relativeIdx] = array[i];
                array[i] = temp;
                relativeIdx++;
            }
        }
        return relativeIdx;
    }

    public static void notRelativeSort(int[] array, int relativeIdx) {
        if (relativeIdx > array.length) {
            return;
        }
        Arrays.sort(array, relativeIdx, array.length);
    }

    public static void fillRelativeArray(int[] array, int[] relative, Map<Integer, Integer> relativeMap) {
        int fillStartIdx = 0;
        for (int i = 0; i < relative.length; i++) {
            Integer toIndex = relativeMap.get(relative[i]) + fillStartIdx;
            Arrays.fill(array, fillStartIdx, toIndex, relative[i]);
            fillStartIdx = toIndex;
        }
    }
}

代碼詳情可點擊查看 我的 GitHub 倉庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章