function [valueTable, decisionTable] = FindPolicyFiniteRW(transMatrix, ...
    payoffs, timeHorizon, discount)

% make sure inputs are columns
payoffs = payoffs(:);
numStates = length(payoffs);
valueTable = zeros(numStates, timeHorizon+1);
decisionTable = zeros(numStates, timeHorizon+1);
% precompute invariant discounted probs 
dprobs = discount*transMatrix;
% initialize 
valueTable(:,timeHorizon+1) = payoffs;
decisionTable(:,timeHorizon+1) = ones(numStates,1);
for t = timeHorizon:-1:1 
    valueWait = dprobs * valueTable(:, t+1); 
    valueTable(:,t) = max(valueWait, payoffs);
    decisionTable(:,t) = valueWait <= payoffs;
end

