-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcp-012-staircase.linq
181 lines (138 loc) · 3.34 KB
/
dcp-012-staircase.linq
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.Numerics.dll</Reference>
<Namespace>System.Numerics</Namespace>
</Query>
// This problem was asked by Amazon.
//
// There exists a staircase with N steps, and you can climb up either 1 or 2 steps
// at a time. Given N, write a function that returns the number of unique ways
// you can climb the staircase. The order of the steps matters.
//
// For example, if N is 4, then there are 5 unique ways:
//
// 1, 1, 1, 1
// 2, 1, 1
// 1, 2, 1
// 1, 1, 2
// 2, 2
//
// What if, instead of being able to climb 1 or 2 steps at a time, you could climb
// any number from a set of positive integers X? For example,
// if X = { 1, 3, 5 }, you could climb 1, 3, or 5 steps at a time.
void Main()
{
//f(4).Select(x => string.Join(", ", x)).Dump();
//f(27).Select(x => string.Join(", ", x)).Dump(); // 2 sec
//f_plus_wrapped(27).Select(x => string.Join(", ", x)).Dump(); // 0.25 sec
//f_plus_wrapped(30).Select(x => string.Join(", ", x)).Dump(); // 0.25 sec
//f_plus_plus(30).Dump(); // 0.05 sec
//f_plus_plus(40).Dump(); // 7.5 sec
//g(40).Dump(); // 0.001
//g(50).Dump(); // 0.005
//g(1000).Dump(); // 0.010
//h(1000).Dump(); // 0 sec :)
//h(10).Dump(); // 0 sec :)
// AHA that's FIBBONACHI SEQUENCE
for (int i = 1; i < 500; i++)
h(i).Dump();
}
public List<List<int>> f(int n)
{
int[] possible = new int[] { 1, 2, };
List<List<int>> ways = new List<List<int>>();
foreach (var step in possible)
{
if (step > n)
continue; // too big of a step
if (step == n)
{
ways.Add(new List<int>() { step });
continue;
}
List<List<int>> subways = f(n - step);
foreach (var subway in subways)
{
var copy = subway.ToList();
copy.Add(step);
ways.Add(copy);
}
}
return ways;
}
public List<List<int>> f_plus_wrapped(int n)
{
List<List<int>> results = new List<List<int>>();
f_plus(n, new List<int>(), results);
return results;
}
public void f_plus(int n, List<int> current, List<List<int>> results)
{
int[] possible = new int[] { 1, 2, };
foreach (var step in possible)
{
if (step > n)
continue;
if (step == n)
{
var copy = current.ToList();
copy.Add(step);
results.Add(copy);
continue;
}
// recoursion dive
current.Add(step);
f_plus(n - step, current, results);
current.RemoveAt(current.Count - 1);
}
}
public int f_plus_plus(int n)
{
int[] possible = new int[] { 1, 2, };
int ways = 0;
foreach (var step in possible)
{
if (step > n)
continue;
if (step == n)
{
ways += 1;
continue;
}
// recoursion dive
ways += f_plus_plus(n - step);
}
return ways;
}
public long g(int n, Dictionary<long, long> cache = null)
{
// dynamic programming based, do not solve solved task over and over again, cache the results!
if (cache == null)
cache = new Dictionary<long, long>();
if (cache.ContainsKey(n))
return cache[n];
int[] possible = new int[] { 1, 2, };
long ways = 0;
foreach (var step in possible)
{
if (step > n)
continue;
if (step == n)
{
ways += 1;
continue;
}
// recoursion dive
ways += g(n - step, cache);
}
cache[n] = ways;
return ways;
}
public BigInteger h(int n)
{
BigInteger[] r = new BigInteger[n + 2];
r[1] = 1; // there is 1 single way to express 1
r[2] = 2; // 2 and 1, 1
for (int i = 3; i <= n; i++)
r[i] = r[i - 1] + r[i - 2];
return r[n];
}