Queue Reconstruction by Height šŸŽÆ

beginner
17 min

Queue Reconstruction by Height šŸŽÆ

Welcome to our deep dive into the world of Data Structures and Algorithms! Today, we're going to learn about Queue Reconstruction by Height, a fascinating problem that combines our understanding of queues, arrays, and heights šŸ“!

Understanding the Problem

Imagine you're at a concert where people of different heights are standing in a queue. Your task is to reconstruct this queue such that every pair of adjacent people has the same height or one person is taller than the other. Let's break it down!

Queue Basics šŸ“

A queue is a data structure that follows a First-In-First-Out (FIFO) principle. This means that the first element that goes into the queue is the first one to come out. In our problem, people join the queue one by one, and we have to ensure the rules are followed when we reconstruct the queue.

Data Structure for the Solution

To solve the problem, we'll use an array to store people's heights and another array to store their positions in the final queue. Here's how we'll represent them:

  1. Height Array: heights[]
  2. Position Array: positions[]

Algorithm

  1. Initialize heights[] and positions[] arrays with the given heights of people.
  2. Sort the heights[] array in ascending order.
  3. Initialize an integer variable next_taller to point to the position of the next taller person in the positions[] array.
  4. For each height in the sorted heights[] array:
    • If next_taller is either empty or the height at next_taller is less than the current height, assign the current index to next_taller.
    • If next_taller is not empty, and the height at next_taller is equal to the current height, increment both next_taller and the current index.
    • If next_taller is not empty, and the height at next_taller is greater than the current height, assign the current index to the position at next_taller and increment both next_taller and the current index.
  5. Once we've gone through all heights, the positions[] array will store the correct order of people in the final queue.

Code Examples šŸ’”

Let's see a simple implementation of the algorithm in Python and Java:

Python

python
def reconstructQueue(heights): # Initialize position array with same length as heights positions = [0] * len(heights) # Sort heights in ascending order heights.sort() # Initialize next_taller to the position of the first person next_taller = 0 # Iterate through sorted heights for i in range(len(heights)): # If next_taller is empty or the current height is greater, update next_taller if next_taller == 0 or heights[i] > heights[next_taller]: next_taller += 1 # If next_taller is not empty, and the current height is the same as the one at next_taller, increment both if next_taller and heights[i] == heights[next_taller]: next_taller += 1 positions[i] = next_taller # If next_taller is not empty, and the current height is greater than the one at next_taller, update positions if next_taller and heights[i] > heights[next_taller - 1]: positions[next_taller - 1] = i next_taller += 1 return positions

Java

java
import java.util.Arrays; public class Main { public static int[] reconstructQueue(int[] heights) { int n = heights.length; int[] positions = new int[n]; Arrays.sort(heights); int nextTaller = 0; for (int i = 0; i < n; i++) { if (nextTaller == 0 || heights[i] > heights[nextTaller]) nextTaller++; if (nextTaller < n && heights[i] == heights[nextTaller]) positions[i] = nextTaller++; if (nextTaller < n && heights[i] > heights[nextTaller - 1]) { positions[nextTaller - 1] = i; nextTaller++; } } return positions; } public static void main(String[] args) { int[] heights = {19, 10, 7, 15, 17, 18, 5, 8, 3, 6, 12, 9, 11, 4, 13, 20, 14, 16}; int[] result = reconstructQueue(heights); // Print the final queue for (int height : result) { System.out.print(height + " "); } } }

Quiz šŸ“

Quick Quiz
Question 1 of 1

What is the data structure that follows the First-In-First-Out (FIFO) principle?

That's it for today! We hope you enjoyed learning about Queue Reconstruction by Height. Stay tuned for more fascinating lessons on Data Structures and Algorithms here at CodeYourCraft šŸš€!