LeetCode 1122 | Relative Sort Array | Python Counting Sort and Rank-Map | AlgoYogi
Welcome to AlgoYogi! π Start Your Smart Coding Prep at π https://algoyogi.io We solve LeetCode 1122: Relative Sort Array using two clean Python approaches. Goal: Sort arr1 so that elements appearing in arr2 come first and in the same relative order as arr2. Any leftover elements not in arr2 come after, in ascending order. Approaches: 1) Counting sort - Since values are small, count frequencies of arr1 - Output items following arr2βs order using counts - Append the remaining values in ascending order 2) Rank-map sort key - Build rank map for arr2 - Sort arr1 with key = (rank.get(x, inf), x) π Problem Link: https://leetcode.com/problems/relative-sort-array/description/ --- ### β± Timestamps 0:00 Introduction 0:20 Problem Statement 1:00 Counting Sort Approach 4:00 Rank-Map Custom Sort 21:00 Python Code Walkthrough 27:00 Time and Space Complexity --- ### π‘ Why Watch? - See a fast counting solution ideal for constrained value ranges - Learn a flexible custom sort key pattern using a rank map - Interview-ready, easy to reason about and implement #LeetCode #RelativeSortArray #Python #AlgoYogi #CountingSort #CustomSort
Download
0 formatsNo download links available.