-
Notifications
You must be signed in to change notification settings - Fork 15
/
Ladder.wrong.cs
58 lines (48 loc) · 1.39 KB
/
Ladder.wrong.cs
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
// This is a Naive solution not using Fibonnacci, it uses the Combinatorial principle behind where k = (positions)! / (1's!)*(2's!)
public static int[] solution(int[] A, int[] B)
{
ulong[] cache = new ulong[30001];
int[] result = new int[A.Length];
for (int i = 0; i < A.Length; i++)
{
ulong positionResult;
if (cache[A[i]] > 0)
{
positionResult = cache[A[i]];
}
else
{
positionResult = CalculatePermutations(A[i]);
cache[A[i]] = positionResult;
}
result[i] = (int)(positionResult % Math.Pow(2, int.MaxValue));
}
return result;
}
public static ulong CalculatePermutations(int ladderSize)
{
if (ladderSize == 1)
{
return 1;
}
ulong combinationsResult = 0;
int maxIterations = ladderSize / 2 + ladderSize % 2;
for (int i = ladderSize; i >= maxIterations; i--)
{
int twosAmount = ladderSize - i;
int onesAmount = i - twosAmount;
ulong combinations = factorial(i) / (factorial(onesAmount) * factorial(twosAmount));
combinationsResult += combinations;
}
return combinationsResult > 0 ? combinationsResult : 1;
}
public static ulong factorial(int number)
{
ulong result = (ulong)number;
for (int i = 1; i < number; i++)
{
result = result * (ulong)i;
}
return result > 0 ? result : 1;
}
}