-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1227-AirplaneSeatAssignmentProbability.go
More file actions
50 lines (42 loc) · 1.5 KB
/
1227-AirplaneSeatAssignmentProbability.go
File metadata and controls
50 lines (42 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
// 1227. Airplane Seat Assignment Probability
// 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 <= 10^5
import "fmt"
func nthPersonGetsNthSeat(n int) float64 {
// 1/n p1 sits correctly
// 1/n p1 takes pn's seat
// (n-2)/n * 1/2 pn gets his seats when p1 is in some else's seat
// 1/2 becoz the poor passenge pi either sits at p1 or pn
// if his seat is taken by p1
if n == 1 {
return 1.0
}
return 0.5
}
func main() {
// Example 1:
// Input: n = 1
// Output: 1.00000
// Explanation: The first person can only get the first seat.
fmt.Println(nthPersonGetsNthSeat(1)) // 1.00000
// 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).
fmt.Println(nthPersonGetsNthSeat(2)) // 0.50000
}