% get_Lmax() - compute maximum L needed to end up with all possible
% paths/costs at the end of the Viterbi algorithm.
%
function Lmax = get_Lmax()

global K;
global MM;
global N;
global combos;
global unique_perms;
global MM_max;

% At time j=1, only states corresponding to actual measurements are allowed,
% and only permutation #1 for K>1.
%
% For 1 < j < N, the number of states is MM(j)+1 for K=1; for K>1 the number
% of feasible states and permutations depends on the number of combinations
% which include actual measurement indices and missed target states, and it
% also depends on the number of unique permutations for each combination.

% N<2 is a degenerate case.  Viterbi just initializes, then is finished.
%
if( N < 2)
  Lmax = 1;
  return;
end

% for K==1, Lmax depends only on MM
%
if( K == 1)
  Lmax = MM(1);
  if( N > 2)
    Lmax = Lmax * prod(MM(2:N-1)+1);
  end
  return;
end

% for K>1, Lmax depends on the numbers of feasible combinations and permutations.
%
up = sum( unique_perms ~= 0)';
Lmax = (sum( combos <= MM(1)) == K) * up;
if( N > 2)
  for j=2:N-1
    Lmax = Lmax * ((sum((combos <= MM(j)) | (combos == MM_max+1)) == K) * up);
  end
end
