-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC119.java
More file actions
43 lines (33 loc) · 1.15 KB
/
Copy pathLC119.java
File metadata and controls
43 lines (33 loc) · 1.15 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
import java.util.ArrayList;
import java.math.BigInteger;
class Solution {
private BigInteger factorial(int num) {
BigInteger result = BigInteger.ONE;
for(int i=2; i<=num; i++)
result = result.multiply(BigInteger.valueOf(i));
return result;
}
private Integer calculateValue(int i, int j) {
BigInteger numerator = factorial(i);
BigInteger denominator = factorial(j).multiply(factorial(i-j));
BigInteger result = numerator.divide(denominator);
return result.intValue();
}
public List<Integer> getRow(int rowIndex) {
List<Integer> row = new ArrayList<>();
int rowMidIndex = (int)Math.floor(rowIndex/2);
for(int i=0; i<=rowMidIndex; i++) {
// Integer element = calculateValue(rowIndex, i);
// row.add(element);
row.add(calculateValue(rowIndex, i));
}
for(int i=rowIndex-rowMidIndex-1; i>=0; i--) {
// Integer element(row.get(i));
// row.add(element);
row.add(row.get(i));
}
return row;
}
}
//Sovled
//Runtime and Memory needs optimizations