美文网首页
leetcode-Array篇easy难度之数组指定顺序排序

leetcode-Array篇easy难度之数组指定顺序排序

作者: 茉莉清可乐对奶茶i | 来源:发表于2020-11-07 16:15 被阅读0次

    关键词

    计数排序,指定顺序排序,TreeMap,countsort
    计数排序讲解 https://www.cnblogs.com/kyoner/p/10604781.html

    题目描述

    https://leetcode.com/problems/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 distinct.
    Each arr2[i] is in arr1.
    

    博主提交的代码

    class Solution {
        public int[] relativeSortArray(int[] arr1, int[] arr2) {
            Map<Integer,Integer> cache = new HashMap<>();
            for(int each: arr2){
                cache.put(each,0);
            }
            for(int i = 0; i < arr1.length; i++){
                if( cache.get(arr1[i]) != null){
                    cache.put(arr1[i],cache.get(arr1[i]) + 1);
                    arr1[i] = -1;
                }
            }
            Arrays.sort(arr1);
            
            int index = 0;
            for(int each: arr2){
                int count  = cache.get(each);
                while(count > 0){
                    arr1[index] = each;
                    index++;
                    count--;
                }
            }
            return arr1;
        }
    }
    

    其他人优秀的代码

    解法一

    计数排序,这东西真的费内存,但是运行时间真的牛逼
    https://leetcode.com/problems/relative-sort-array/discuss/335056/Java-in-place-solution-using-counting-sort

    class Solution {
        public int[] relativeSortArray(int[] arr1, int[] arr2) {
            int[] cnt = new int[1001];
            for(int n : arr1) cnt[n]++;
            int i = 0;
            for(int n : arr2) {
                while(cnt[n]-- > 0) {
                    arr1[i++] = n;
                }
            }
            for(int n = 0; n < cnt.length; n++) {
                while(cnt[n]-- > 0) {
                    arr1[i++] = n;
                }
            }
            return arr1;
        }
    }
    

    解法二

    https://leetcode.com/problems/relative-sort-array/discuss/335217/Java-O(n*lgn)-1-ms-beats-100

    public int[] relativeSortArray(int[] arr1, int[] arr2) {
            //create map for counting numbers in arr1. Initialize everything with zeroes
            Map<Integer, Integer> m = new HashMap();
            for (int num : arr2) {
                m.put(num, 0);
            }
            int last = arr1.length - 1;
            int[] res = new int[arr1.length];
            //iterate over arr1  and count numbers of time this element is in arr1
            for (int num : arr1) {
                //if number is from arr2 - increment count
                if (m.containsKey(num))
                    m.put(num, m.get(num) + 1);
                //otherwise add element to the end of res and decrement the pointer
                else {
                    res[last--] = num;
                }
            }
            //iterate over arr2, fill elements in res based on it's count 
            int p = 0;
            for (int num : arr2) {
                int c = m.get(num);
                for (int i = 0; i < c; i++) {
                    res[p++] = num;
                }
            }
            //now sort the leftovers - p points to the first element in series of those from arr2 that are not in arr1 
            Arrays.sort(res, p, res.length);
            return res;
        }
    

    更加极致

    public static int[] relativeSortArrayV1(int[] arr1, int[] arr2) {
            TreeMap<Integer, Integer> freq = new TreeMap<>();
            for (int num : arr1) {
                freq.merge(num, 1, (a, b) -> a + 1);
            }
            int[] res = new int[arr1.length];
            int i = 0;
            for (int key : arr2) {
                int reps = freq.remove(key);
                for (int j = 0; j < reps; j++) {
                    res[i++] = key;
                }
            }
    
            while (freq.size() > 0) {
                int key = freq.firstKey();
                int reps = freq.remove(key);
                for (int j = 0; j < reps; j++) {
                    res[i++] = key;
                }
            }
            return res;
        }
    

    相关文章

      网友评论

          本文标题:leetcode-Array篇easy难度之数组指定顺序排序

          本文链接:https://www.haomeiwen.com/subject/ucwtbktx.html