Prefix State Map
I haven't found an official name of this kind of problem. I just named it as "Prefix State Map".
This kind of problem is usually:
about finding the longest/shortest subarray which satisfies some condition.
the state of the subarray can be deduced using the the states of the prefix array. For example, to get the state of
A[i..j]
, you can use the state of two prefix array,A[0..(i-1)]
andA[0..j]
.
Algorithm
Let m
be a map from the state state[i]
of a prefix subarray A[0..i]
to the index of the first/last occurrence of the state state[i]
.
For the current index i
and its state state[i]
, assume we want to get a subarray which ends at i
and has a target_state
and has the longest/shortest length.
Then we can use state[i]
and target_state
to compute a prefix_state
. j = m[prefix_state]
is the corresponding index of the prefix array. A[(j+1) .. i]
is the longest/shortest optimal subarray.
Pseudo Code
Problems
Last updated