1. Problem
2. My Solution
import sys
def solution(n):
if n in dp:
return dp[n]
else:
dp[n] = solution(n-1) + solution(n-2) + solution(n-3)
return dp[n]
test_n = int(sys.stdin.readline().rstrip())
dp = {}
dp[1] = 1
dp[2] = 2
dp[3] = 4
for _ in range(test_n):
n = int(sys.stdin.readline().rstrip())
print(solution(n))
1. Problem
2. My Solution
import sys
def solution(n):
temp = []
if dp[n] >= 0:
return dp[n]
elif n == 1:
dp[n] = card[n]
return dp[n]
else:
for i in range(1,n+1):
temp.append(solution(n-i) + card[i])
dp[n] = max(temp)
return dp[n]
n = int(sys.stdin.readline().rstrip())
card = [0] + list(map(int,sys.stdin.readline().rstrip().split()))
dp = [0] + [-1] * 1000
print(solution(n))
3. Others' Solution
import sys
def solution(n):
for i in range(1,n+1):
for j in range(1,i+1):
dp[i]= max(dp[i], dp[i-j] + card[j])
n = int(sys.stdin.readline().rstrip())
card = [0] + list(map(int,sys.stdin.readline().rstrip().split()))
dp = [0] + [0] * 1000
solution(n)
print(dp[n])
4. Learned