Reconstructing a Queue with Greedy Approach

🚀 Day 39/100 – LeetCode Challenge Today’s problem: 406. Queue Reconstruction by Height This problem focuses on applying a Greedy approach with sorting to reconstruct a queue based on height and positional constraints. 🔍 Key Insight: Sort people by height in descending order and k value in ascending order, then insert each person at their respective index. 💡 What I learned: Importance of sorting strategy in greedy problems How insertion at a specific index can maintain constraints Thinking from the perspective of "who affects whom" (taller vs shorter) 🧠 Approach: Sort the array → (-height, k) Insert each person at index k 💻 Code (Python): class Solution: def reconstructQueue(self, people): people.sort(key=lambda x: (-x[0], x[1])) queue = [] for p in people: queue.insert(p[1], p) return queue ⏱️ Time Complexity: O(n²) Consistency is the key — showing up every day and improving step by step. #Day39 #LeetCode #100DaysOfCode #DSA #Python #CodingChallenge #GreedyAlgorithm #SoftwareDevelopment

  • text

To view or add a comment, sign in

Explore content categories