-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBinomial.java
More file actions
90 lines (79 loc) Β· 2.1 KB
/
Copy pathBinomial.java
File metadata and controls
90 lines (79 loc) Β· 2.1 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package day4;
import java.util.Scanner;
public class Binomial {
/*
time complexity: O(n^3)
space complexity: O(n)
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
printPascalsTriangle(number);
}
/*
time complexity: 0^2 + 1^2 + 2^2 + 3^2 + ... + rows^2 = O(rows^3)
space complexity: max(i) = max(0, 1, 2, 3, 4, 5, 6 ... rows) = O(rows)
0^2 + 0
1^2 + 1
2^2 + 2
..
..
rows^2 + rows
(o^2 + 1^2 + 2^2 + ... rows^2) + (0 + 1 + 2 + 3 + ... + rows)
O(rows^3) + O(rows^2) = O(rows^3)
*/
private static void printPascalsTriangle(int rows) {
for (int i = 0 ; i < rows ; i++) {
// i^2 = (rows/2)^2
print(binomial(i));
}
}
/*
n = array.length
time complexity: O(n)
space complexity: O(1)
*/
private static void print(int[] array) {
for (int element : array) {
System.out.print(element + " ");
}
System.out.println();
}
/*
time complexity: O(n)
space complexity: O(n)
*/
private static int factorial(int number) {
if (number == 0) {
return 1;
}
return number * factorial(number - 1);
}
// nPr = n! / (n - r)!
/*
time complexity: O(n)
space complexity: max(n, n-r) = O(n)
*/
private static int permutation(int n, int r) {
return factorial(n) / factorial(n - r);
}
// nCr = nPr / r!
/*
time complexity: O(n)
space complexity: max(n, r) = O(n)
*/
private static int combination(int n, int r) {
return permutation(n, r) / factorial(r);
}
/*
time complexity: O(n^2)
space complexity: O(n)
*/
private static int[] binomial(int n) {
int[] result = new int[n + 1];
for (int index = 0 ; index < result.length ; index++) {
result[index] = combination(n, index);
}
return result;
}
}