Comments (3)
I think the solution is already take these cases into account.
Here I replace your k with i for clear explanation according to the parameters I used in the solution.
The first candidate hired must have index = 1, and here you say that it's rank is i (where i = 3). If we directly hire the candidate with index = 2 and its rank is n (where n is the highest), this is one of the condition that
Let E_i be the event in which candidate 1 has rank i (where i = 3).
Since n = 6, the candidates with 3 < rank < 6 (i.e., rank = 4 & 5) must be interviewed after candidate with rank = 6 (We don't care about where candidates with rank 1 and rank 2 are). And the probability is calculated by:
S[All events of the order of the candidates with rank 4, rank 5 and rank 6] = {645, 654, 465, 456, 546, 564}
P[candidates with rank 4 and rank 5 are both after rank 6] = 2 / 6 = 1 / 3.
I think you are confused because you think that there are no candidates between candidate 1 (rank = i) and candidate 2 (rank = n), but this condition is still in the the cases. We mainly focus on one thing: all candidates with "i < rank < n" must be interviewed after the one who has the highest rank n.
Is this clear for you?
from clrs.
thanks for your answer I'll study it as soon I have time.
from clrs.
After review, the solution is actually right, it is just another way to see things. I tried a solution more combinatory but far less efficient even if I get the same result
@Test
public void hireTwiceTestMethod1() {
double total = 0;
//for each rank for the candidate 1 [ 1, n [
for (int i = 1; i <= n - 1; i++) {
//for each number of candidates with a rank < i, checked before the candidate with rank n
for (int b = 0; b <= i - 1; b++) {
//here we have a number of arrangements of b candidates with rank < i
//multiplied by the number of of arrangements of candidates after the one with rank n
//so after the rank n is hired you can have candidates with rank < i and all the ones with rank > i
total += (factorial(i - 1) / factorial(i - 1 - b)) * factorial(n - b - 2);
}
}
double rs = total / factorial(n);
System.out.println(rs);
}
the result is the same than with this procedure from the official solution manual
@Test
public void hireTwiceTestMethod2() {
double rs = nThHarmonicNumber(n - 1) / n;
System.out.println(rs);
}
other procedures
public static double factorial(double n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
public static double nThHarmonicNumber(double n){
double total = 0;
while( n >= 1 ){
total += 1.0 / n;
n --;
}
return total;
}
from clrs.
Related Issues (20)
- 15.4-6
- Can I fix the answer to 24.3-3? HOT 1
- 2.1.c
- Can i fix the answer to 22.2-9? HOT 1
- about 15.4-3
- 16.2-2 HOT 2
- Markdown render error - problems 1-1, 3-2, 3-6 HOT 2
- 1 second equals 1000 milliseconds,f(n)=n.why f(n)=10^6 HOT 5
- about Problem 16-5 HOT 1
- Problem 15-2: More Efficient Algorithm
- Exercise 4.4-4 HOT 1
- Problem 6-2 HOT 2
- 7.4-2
- Solution to 24.4-9 may be wrong
- 5.2-4 HOT 2
- 24.1-5 answer
- 21.3-1 solution incorrect and incomplete
- 15-1 solution incorrect
- Is 6.5.8 wrong?
- Possibly counterexample to 22.2-3 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clrs.