def update_mdp_model_with_history(counts, rewards, history):
    """Given a history of an entire episode, update the count and rewards arrays

    Parameters
    ----------
    counts: array of shape [nS x nA x nS]
        counts[state][action][next_state] is the number of times that doing "action" at state "state" transitioned to "next_state"
    rewards: array of shape [nS x nA x nS]
        counts[state][action][next_state] is the running average of rewards of doing "action" at "state" transtioned to "next_state"
    history: 
        a list of [state, action, reward, next_state, done]
    """

    # HINT: For terminal states, we define that the probability of any action returning the state to itself is 1 (with zero reward)
    # Make sure you record this information in your counts array by updating the counts for this accordingly for your
    # value iteration to work.

    ############################
    # YOUR IMPLEMENTATION HERE #
    for item in history:
        #print item
        (state, action, reward, next_state, done) = item
        #if not done:
        #    counts[state][action][next_state] += 1
        #    rewards[state][action][next_state] = float(rewards[state][action][next_state]+reward) / counts[state][action][next_state]
        #else:
        #    counts[state][action][next_state] = 1
        #    rewards[state][action][next_state] = float(rewards[state][action][next_state]+reward) / counts[state][action][next_state]
        counts[state][action][next_state] += 1
        all_reward = float(rewards[state][action][next_state]*(counts[state][action][next_state]-1)+reward)
        rewards[state][action][next_state] = all_reward / counts[state][action][next_state]
    ############################
    return counts, rewards