function [stateValues, policy, count] = PW311_FindPolicy(probs, payoffs, ...
    discount, myeps, maxIter)
% make sure inputs are columns
probs = probs(:);
payoffs = payoffs(:);
% set default values for tolerance and max iterations
if nargin < 4, myeps=0.00001; end
if nargin < 5, maxIter=1000; end
numStates = length(payoffs);
oldV = zeros(numStates,1);
newV = zeros(numStates,1);
stopit = false;
count = 0; % iteration counter
% precompute invariant discounted probs 
dprobStay = discount*probs;
dprobMove = discount*(1-probs(:));
% we need to make a decision only in interior states
idxInteriorStates = 2:(numStates-1);
% value iteration
while (~stopit)
    count = count + 1;
    % update: first and last state are apart
    newV(1) = dprobStay(1)*oldV(1)+dprobMove(1)*oldV(2);
    newV(numStates) = payoffs(numStates) + discount*oldV(1);
    % now update interior states
    valueReset = payoffs(idxInteriorStates) + discount*oldV(1);
    valueWait = dprobStay(idxInteriorStates).*oldV(idxInteriorStates) + ...
        dprobMove(idxInteriorStates).*oldV(idxInteriorStates+1);
    newV(idxInteriorStates) = max(valueWait, valueReset);
    if ( (norm(newV-oldV,Inf) < myeps) || (count > maxIter) )
        stopit = true;
    else
        oldV = newV;
    end
end
stateValues = newV;
policy = [0; valueReset > valueWait; 1];
