Created
November 8, 2020 13:30
-
-
Save qiuyujx/7afc1b8be2b39f82fea7cc37a6857900 to your computer and use it in GitHub Desktop.
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
def max_vote_dynamic_planning(states, total_days): | |
dp_matrix = [[0 for days_left in range(total_days + 1)] for index in range(len(states) + 1)] | |
for index in range(1, len(states) + 1): | |
for days_left in range(1, total_days + 1): | |
if states[index-1]['days'] <= days_left: # If we have enough days left | |
votes_if_go = dp_matrix[index-1][days_left - states[index-1]['days']] + states[index-1]['votes'] | |
votes_if_not_go = dp_matrix[index-1][days_left] | |
# Save the maximum votes into cache | |
dp_matrix[index][days_left] = max(votes_if_go, votes_if_not_go) | |
else: # We don't have any days left | |
dp_matrix[index][days_left] = dp_matrix[index-1][days_left] | |
return dp_matrix[-1][-1] | |
max_vote_dynamic_planning(states_dict, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment