You are given a binary array state consisting of integers o and 1. You are also given…
- User
You are given a binary array state consisting of integers o and 1. You are also given operations - an array of strings, each representing an operation of one of two types: "L" - find the smallest index for which state [ij = o and set state [i] = 1. If there is no such index, do nothing "c{index}" - set state lindex] = 0 . This operation does not depend on the previous value of state [index] It is guaranteed that index is a valid 0-based index of state (ie: index < state length Given state and operations your task is to return the binary string of state array after all the operations have been applied.
Example • For state - [0, 0, 0, 0, 0, 0, 0, 0, 0, 01 and operations = E'L", "L", "CO", "L", "C3"1 , the output should be solution (state, operations) = "1100000000"
Initially the state is [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]. On the 1st operation "L", the smallest index with state [i] = 0 is i = 0. After this operation, the state is [1, 0, 0, 0, 0, 0, 0, 0, 0, 01. On the and operation "L", the smallest index with state [il = o is i = 1, as state[0] = 1. After this operation, the state is [1, 1, 0, 0, 0, 0, 0, 0, 0, 0] On the 3rd operation "co, we set state [0] = 0 . After this operation, the state is [0, 1, 0, 0, 0, 0, 0, 0, 0. 0] On the 4th operation the smallest index with state [i] = o is - 0 After this operation, the state is [1, 1, 0, 0, 0, 0, 0, 0, 0, 01 On the 5th operation we set state [3] - 0. After this operation, the state is [1, - 1, 0, 0, 0. 0, 0, 0, 0, 0] All the operations are done so return 1100000000* as the result. For state = [1, Oi and operations = ['L", "L", "CI"], the output should be solution (state, operations) = "10'
Initially the state is [1, 01 On the 1st operation "L", the smallest index with state [if = 0 is i = 1 . After this operation, the state is [1, 1]. On the 2nd operation "L", the operation should be ignored, as there is no index with state[i] = 0 . After this operation, the state is [1, 1] . On the 3rd operation "ci' , we set state [1] = 0 . After this operation, the state is [1, 0] All the operations are done so return "10" as the result.
Input/Output [execution time limit] 0.5 seconds (pp) [memory limit] 1 GB [input] array. integer state An integer array representing the initial values Guaranteed constraints. 1 ≤ state. length ≤ 105 [input] array.string operations An array of strings representing the operations applied to the array Each operation is of one of the two types listed in the description Guaranteed constraints: 1 s operations.length ≤ 5 - 105 [output] string The resulting binary string of state after all the operations have been applied.
Write in cpp
- Assistant