g1201_1300.s1227_airplane_seat_assignment_probability.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g1201_1300.s1227_airplane_seat_assignment_probability;
// #Medium #Dynamic_Programming #Math #Brainteaser #Probability_and_Statistics
// #2022_03_12_Time_1_ms_(15.63%)_Space_41.7_MB_(12.95%)
/**
* 1227 - Airplane Seat Assignment Probability\.
*
* Medium
*
* `n` passengers board an airplane with exactly `n` seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of the passengers will:
*
* * Take their own seat if it is still available, and
* * Pick other seats randomly when they find their seat occupied
*
* Return _the probability that the_ nth
_person gets his own seat_.
*
* **Example 1:**
*
* **Input:** n = 1
*
* **Output:** 1.00000
*
* **Explanation:** The first person can only get the first seat.
*
* **Example 2:**
*
* **Input:** n = 2
*
* **Output:** 0.50000
*
* **Explanation:** The second person has a probability of 0.5 to get the second seat (when first person gets the first seat).
*
* **Constraints:**
*
* * 1 <= n <= 105
**/
public class Solution {
public double nthPersonGetsNthSeat(int n) {
if (n == 1) {
return 1.0D;
}
return 0.5D;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy