-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterminant.cpp
More file actions
40 lines (36 loc) · 939 Bytes
/
determinant.cpp
File metadata and controls
40 lines (36 loc) · 939 Bytes
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
/*
4 kyu
Matrix Determinant
https://www.codewars.com/kata/52a382ee44408cea2500074c
*/
#include <vector>
using LL = long long;
using LLMatrix = std::vector<std::vector<LL>>;
static LLMatrix create_sub_matrix(const LLMatrix& m, size_t col) {
size_t size = m.size();
size_t newsize = size - 1;
LLMatrix sub(newsize, std::vector<LL>(newsize));
for (size_t r = 1; r < size; ++r) {
size_t sub_col = 0;
for (size_t c = 0; c < size; ++c) {
if (c == col)
continue;
sub[r - 1][sub_col++] = m[r][c];
}
}
return sub;
}
LL determinant(const LLMatrix& m) {
size_t size = m.size();
if (size == 1)
return m[0][0];
if (size == 2)
return m[0][0] * m[1][1] - m[1][0] * m[0][1];
LL result = 0;
for (size_t i = 0; i < size; ++i) {
LLMatrix sub = create_sub_matrix(m, i);
LL cofactor = ((i % 2 == 0) ? 1 : -1) * m[0][i] * determinant(sub);
result += cofactor;
}
return result;
}