-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from time import time | ||
from matplotlib import pyplot as plt | ||
|
||
|
||
def fib_recursive(n): | ||
if n < 2: | ||
return n | ||
return (fib_recursive(n - 1) + fib_recursive(n - 2)) % 10**9 | ||
|
||
|
||
fibonacci_mem = {} | ||
|
||
|
||
def fib_mem(n): | ||
if n < 2: | ||
return n | ||
if n not in fibonacci_mem: | ||
fibonacci_mem[n] = (fib_mem(n - 1) + fib_mem(n - 2)) % 10 ** 9 | ||
return fibonacci_mem[n] | ||
|
||
|
||
def fib_tab(n): | ||
res = [0] * max((n + 1), 2) | ||
res[0] = 0 | ||
res[1] = 1 | ||
for i in range(2, n + 1): | ||
res[i] = (res[i - 1] + res[i - 2]) % 10**9 | ||
return res[n] | ||
|
||
|
||
if __name__ == '__main__': | ||
times = [] | ||
N_max = 35 | ||
step = 1 | ||
for i in range(1, N_max, step): | ||
t = time() | ||
|
||
# naive solution: | ||
x = fib_recursive(i) | ||
|
||
# memoization: | ||
# cache is resettet to calculate correct time for each i | ||
# otherwise it will take old values from cache: | ||
# fibonacci_mem = {} | ||
# x = fib_mem(i) | ||
|
||
# tabulation: | ||
# x = fib_tab(i) | ||
|
||
times.append(time() - t) | ||
print(f'N={i}: {times[-1]:.8f} sec.') | ||
|
||
plt.plot(times) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def lis_n2(x): | ||
N = len(x) | ||
d = [0] * N | ||
d[0] = 1 | ||
for i in range(1, N): | ||
# don't need to find j | ||
# Need just to calculate maxd: | ||
max_d = 0 | ||
for j in range(i): | ||
if x[j] < x[i] and d[j] > max_d: | ||
max_d = d[j] | ||
d[i] = max_d + 1 | ||
return max(d) | ||
|
||
|
||
if __name__ == '__main__': | ||
N = int(input()) | ||
x = list(map(int, input().split())) | ||
print(lis_n2(x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from time import time | ||
|
||
|
||
def stairs_dp(s): | ||
N = len(s) | ||
d = [None] * N | ||
d[0] = s[0] | ||
d[1] = s[1] | ||
for i in range(2, N): | ||
d[i] = min(d[i - 1], d[i - 2]) + s[i] | ||
return d[N - 1] | ||
|
||
|
||
def stairs_rec(s): | ||
if len(s) <= 2: | ||
return s[-1] | ||
d1 = stairs_rec(s[1:]) + s[0] | ||
d2 = stairs_rec(s[2:]) + s[1] | ||
return min(d1, d2) | ||
|
||
|
||
if __name__ == '__main__': | ||
N = int(input()) | ||
s = list(map(int, input().split())) | ||
t1 = time() | ||
print(stairs_dp(s)) | ||
t2 = time() | ||
print(stairs_rec(s)) | ||
t3 = time() | ||
dp_time = t2 - t1 | ||
rec_time = t3 - t2 | ||
print(f'Naive: {rec_time:.8f} sec') | ||
print(f'DP: {dp_time:.8f} sec') |