Given a binary string S and a string of non-zero digits P of length N, the task is to maximize the sum using the given operations, which can be used as my times you can:
You can choose a substring of length 2 or 3.
Delete that substring after calculating the absolute difference of adjacent elements from left to right and take any single digit from the string P and multiply that with the absolute difference obtained by chosen substring.
Input: N = 4, S = 1001, P = 6574 Output: 13 Explanation: For string S = 1001, choose a substring from index 1 to 2 assubstring= 10. Absolute difference = |1 – 0| = 1 Multiply it with the digit 7 of string P = 7*1 = 7. Now chosen substring (10) is deleted from string (1001) and remaining string is = 01. Then, choose a substring from index 1 to 2 as substring = 01. Absolute difference = |0-1| = 1, Multiply it with the 6 of string P = 6*1 = 6. Now substring is deleted and string S is empty, and total maximum sum that can obtain is 7+6 = 13.
Input: N = 4, S = 1111, P = 1221 Output: 2 Explanation: For, string S = 1111, choose a sub-array from index 1 to 3 as sub-array = 111. Absolute difference of first left two elements of chosen substring = |1-1| = 0, Now substring is = 01.(By replacing obtained abs. difference with first two elements). Now, absolute difference of first left two elements of current sub-array (01) = |0-1| = 1. Total absolute difference for this substring is 1. Take digit 2 from P and multiply it with obtained absolute difference = 1*2= 2. Now delete this substring(111) from S(1111), Then remained S is = 1. We cannot make any operations further, Because S has single element and have not any adjacent element. Therefore, Maximum sum that can obtain is = 2.
Approach: Implement the idea below to solve the problem:
The problem can be solved by using Stack data structure and greedy approach to choose a digit from string P for multiplication. For knowing the exact algorithm to solve the problem see the Algorithm section below.
Follow the steps to solve the problem:
Initialize a Stack (say stk) and create a counter and sum variables.
Make an ArrayListlets call it digits to store digits of string P and sort it.
Run a loop from 1 to N and do
If stk is empty or the top of stk = current_element, push the typecasted integer element in the stack.
Else if top of stk is not the same as current_element, pop the peek element from stk and add (1*last element of digits) into sum variable also remove used digits from the list.
Now it is possible that some element remained in the stack. Therefore, run a while loop till the stk has no element in it. Count the number of 1 remaining in the stk in the counter variable.
Modify the counter variable’s value now with (counter/3). The reason is that 3 ones can make absolute difference as 1, same as the input 2 in the example above.
Do the same, add (1*last element of digits ArrayList) into sum variable also remove used digits from the list, while counter is greater than 0.
Print the value of the sum variable.
Below is the implementation of the above approach.
Please Login to comment...