Recently , I came across this question:
We will be given an array consisting of only 0’s and 1’s.We need to make array empty by doing this operation:
In one move,you can select some consecutive equal elements from this array and remove them.The remaining part of the array(if there are more than one)just get attached back together.For example,if you remove [0,0] from [1,0,0,1],the resulting array is [1,1]
Determine the minimum number of moves required to remove all elements.
Note:Elements can be removed one by one if no consecutive equal numbers are found.
input: [1,0,0,1] output:2
explanation: remove [0,0] from array,then array would be [1,1]. Removing [1,1] makes array length=0 hence two operations.
explanation:The steps are [0,1,0,1]->[1,0,1]->[1,1]-> Therefore the answer is 3.
Can anyone help me in approaching this problem and how to solve such type of problems?