Back to Browse

LeetCode 1122 | Relative Sort Array | Python Counting Sort and Rank-Map | AlgoYogi

12 views
Oct 24, 2025
29:41

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 formats

No download links available.

LeetCode 1122 | Relative Sort Array | Python Counting Sort and Rank-Map | AlgoYogi | NatokHD