/
31.lua
53 lines (47 loc) · 869 Bytes
/
31.lua
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
coins = {200, 100, 50, 20, 10, 5, 2, 1 }
results = {}
function memget(total, sm)
if results[sm]~=nil then
return results[sm][total]
end
return nil
end
function memset(total, sm, value)
if results[sm]==nil then
results[sm] = {}
end
results[sm][total] = value
end
function fast(total, sm)
sm = sm or 8
local sum = memget(total, sm)
if sum==nil then
sum = 0
for i = 1, sm do
local coin = coins[i]
if coin == total then
sum = sum + 1
elseif coin < total then
sum = sum + fast(total-coin, i)
end
end
memset(total, sm, sum)
end
return sum
end
function slow(total, sm)
local sum = 0
sm = sm or 8
for i = 1, sm do
local coin = coins[i]
if coin == total then
sum = sum + 1
elseif coin < total then
sum = sum + slow(total-coin, i)
end
end
return sum
end
-- print(slow(200))
print(fast(200))
-- print(slow(200))