forked from gabrielgiordan/hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s10-mcq-3.js
55 lines (37 loc) · 946 Bytes
/
s10-mcq-3.js
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
// x, y, z
let arr = [[4, 3], [5, 4], [4, 4]]
function gcd(a, b) {
if (!b) return a
return gcd(b, a % b)
}
function probability(arr) {
let r = []
for (let i = 0; i < arr.length; i++) {
let s = arr[i].reduce((a, b) => a + b)
r[i] = []
for (let j = 0; j < arr[i].length; j++) {
let g = gcd(arr[i][j], s)
r[i][j] = [arr[i][j] / g, s / g]
}
}
let a = [[],[]]
for (let i = 0; i < r.length; i++) {
a[0][i] = []
a[1][i] = []
for (let j = 0; j < r.length; j++) {
// 0 0 1 0 1 0 1 0 0
// x y z x y z x y z
let k = i - j === 0 ? 1 : 0
a[0][i][j] = r[j][k][0]
a[1][i][j] = r[j][k][1]
}
a[0][i] = a[0][i].reduce((a, b) => a * b)
a[1][i] = a[1][i].reduce((a, b) => a * b)
}
let ra = a[0].reduce((a, b) => a + b),
rb = a[1][0],
gr = gcd(ra, rb)
r = ra / gr + '/' + rb / gr
return r
}
console.log(probability(arr))