You are given an integer array values, a binary string state of the same length, and an integer m such that 0 <= m <= values.length.
Build a sequence s of length m using exactly m operations. You may assume it is always possible to complete all m picks.
In each operation:
-
Choose any index
i
such that
state[i] = '1'
.
-
Append
values[i]
to
s
.
-
Mark that index as unavailable by setting
state[i] = '0'
.
-
Then perform one simultaneous availability update on this new state: for every index
j > 0
, if
state[j - 1] = '1'
and
state[j] = '0'
, flip
state[j]
to
1
.
Return the lexicographically largest possible sequence s. For integer sequences, lexicographic order compares elements from left to right.
Example:
-
values = [10, 5, 7, 6]
-
state = "0101"
-
m = 2
One optimal answer is [6, 7].
Write a function to compute the lexicographically largest sequence.