LeetCode 1204: Last Person to Fit in the Bus Solution
Master LeetCode problem 1204 (Last Person to Fit in the Bus), 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.
1204. Last Person to Fit in the Bus
Problem Explanation
Explanation
To solve this problem, we need to iterate through the queue of people in the order of their turn and keep track of the total weight of people boarding the bus. We will stop iterating when the total weight exceeds the limit of 1000 kilograms. The last person who can fit on the bus will be the person before the total weight exceeds the limit.
Algorithm:
- Sort the queue of people based on their turn in ascending order.
- Iterate through the sorted queue and keep track of the total weight.
- Stop iterating when the total weight exceeds 1000 kilograms.
- Return the name of the person before the weight limit is exceeded.
Time Complexity: O(nlogn) - Sorting the queue with n people Space Complexity: O(1) - Constant extra space is used
Solution Code
# Write your Java solution hereTry It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1204 (Last Person to Fit in the Bus)?
This page provides optimized solutions for LeetCode problem 1204 (Last Person to Fit in the Bus) 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 1204 (Last Person to Fit in the Bus)?
The time complexity for LeetCode 1204 (Last Person to Fit in the Bus) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1204 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1204 in Java, C++, or Python.