1122. Relative Sort Array

In this problem we are given 2 arrays where the first array contains all the numbers that the second array has but the second array does not contain all. In mathematics we call array1 to be the superset of array2 and array2 to be the subset of array1. And we are being asked to reproduce an array of elements where each number preservers the index found in the array2. That being said, any number that is not in the subset then we will add to the end of the result array.

Approach

. Repeated Numbers

The numbers in the array are not sorted and the numbers are allowed to repeat itself. If we are asked to maintain the order of the numbers found on array 2 then if a number is repeated more than once we should include the number at i + 1 index from the original number. The best thing that comes in mind is using a Hash Table where we can easily record recurrence and store the numbers at O(n) times where n is the length of the array1.

. Preserving the Order

Now that we have our map object we can loop through the length of array2 and at each iteration check if the number at ith index exist in the map. If if exist, enter a inner loop to add the repeated number at i + 1 index from the original number. The order is preserved since we are using the ith number on array2, starting at index 0, as a pivot to the insertion in the result array at the same index of array2.

. Dealing with Remaining Number

Once we inserted the number k times, where k is the number of times the number repeats, we can delete the key from the map. Once we exit the iteration the map will contain only the keys (number) that array2 does not contain. In the 3r iteration we can simply add the keys at k times that correspond to the value in the map. However, since the elements not included in the second array must be sorted, we should store in another array to then sort it in ascending order.

Result

The result should be all the numbers and the remaining numbers added to the end of the result array in ascending order


Time Complexity

The first iteration where we construct the map is O(n) times where n is the length of the first array. The second iteration and verifying if value exist is O(m) where m is the length of the second array. The prototype.has() has O(1) time complexity. The remaining is O(m-n) since is the difference of lengths from both, And finally, the sorting is O(logn).

O(1) + O(n) + O(m) + O(m-n) + O(logn) = O(nlogn)



要查看或添加评论,请登录

Mohamed Sharif的更多文章

  • A Solution for DNS Leaks Using Kali Linux

    A Solution for DNS Leaks Using Kali Linux

    Disclaimer: This is for educational purposes to learn how to properly configure a vpn. This should not be used with the…

  • An Easier explanation for AVL Trees And Implemented Using Python

    An Easier explanation for AVL Trees And Implemented Using Python

    How To Find Parent And Children When An Array Is Given AVL trees might seem more complicated than what it seems but if…

  • Implementing A Binary Heap Using Python

    Implementing A Binary Heap Using Python

    Binary heaps are a form of binary tree but the only order that we follow is that the parent value should be either…

  • Identifying Node's Relations From Array For A Complete Binary Tree

    Identifying Node's Relations From Array For A Complete Binary Tree

    I would like to make this article more conceptual and to revise the relation of the current node value to its left…

  • Front End Development And Integration With Google API | Using Hooks, Throttle, And Dynamic Style.

    Front End Development And Integration With Google API | Using Hooks, Throttle, And Dynamic Style.

    This article can be better understood if you are an intermediate React and familiar with API Calls. In this article, I…

  • 1064. Fixed Point

    1064. Fixed Point

    https://leetcode.com/problems/fixed-point/solutions/5862817/use-binary-search-and-if-value-found-recurse-to-the-left-sin…

  • Odd Even Linked List

    Odd Even Linked List

    In this article I would like to go over a solution to solve leet code problem 328. Odd Even Linked List.

  • 461. Hamming Distance

    461. Hamming Distance

    In this article we are going through the Leet Code Hamming Distance problem. We are given 2 integer numbers and we are…

  • Leaf-Similar Trees

    Leaf-Similar Trees

    I will be walking through the algorithm to solve the problem of 2 nodes where we are told to find if the leaf of both…

  • Solving Leet Code 243. Shortest Word Distance

    Solving Leet Code 243. Shortest Word Distance

    In this problem, we are given a array with words and there are 2 words that already exist and the goal is to return the…

社区洞察

其他会员也浏览了