LeetCode 2783: Flight Occupancy and Waitlist Analysis Solution

Master LeetCode problem 2783 (Flight Occupancy and Waitlist Analysis), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2783. Flight Occupancy and Waitlist Analysis

Medium

Problem Explanation

Explanation:

To solve this problem, we can simulate the process of assigning seats to passengers based on their preferences. We need to consider both the waitlist of passengers and the current occupancy status of the flight. The key idea is to iterate through the waitlist and assign seats to passengers based on their seating preferences and the availability of seats. We can use a data structure like a priority queue to efficiently handle the assignment process.

  1. Initialize a priority queue to store passengers from the waitlist based on their priority (e.g., first-class, business class, economy class).
  2. Iterate through the waitlist and assign seats to passengers based on their preferences and seat availability.
  3. Update the occupancy status of the flight based on the assigned seats.
  4. Return the final occupancy status of the flight.

Time complexity: O(n log n) where n is the number of passengers in the waitlist. Space complexity: O(n) for the priority queue.

:

Solution Code

import java.util.*;

class Solution {
    public int[] flightOccupancy(int totalSeats, int[] waitlist) {
        PriorityQueue<Integer> firstClass = new PriorityQueue<>();
        PriorityQueue<Integer> businessClass = new PriorityQueue<>();
        PriorityQueue<Integer> economyClass = new PriorityQueue<>();

        for (int passenger : waitlist) {
            if (passenger == 1) {
                firstClass.offer(passenger);
            } else if (passenger == 2) {
                businessClass.offer(passenger);
            } else {
                economyClass.offer(passenger);
            }
        }

        int[] occupancy = new int[totalSeats];
        Arrays.fill(occupancy, -1);

        for (int i = 0; i < totalSeats; i++) {
            if (!firstClass.isEmpty()) {
                occupancy[i] = firstClass.poll();
            } else if (!businessClass.isEmpty()) {
                occupancy[i] = businessClass.poll();
            } else if (!economyClass.isEmpty()) {
                occupancy[i] = economyClass.poll();
            }
        }

        return occupancy;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2783 (Flight Occupancy and Waitlist Analysis)?

This page provides optimized solutions for LeetCode problem 2783 (Flight Occupancy and Waitlist Analysis) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2783 (Flight Occupancy and Waitlist Analysis)?

The time complexity for LeetCode 2783 (Flight Occupancy and Waitlist Analysis) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2783 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2783 in Java, C++, or Python.

Back to LeetCode Solutions