5 kyu

Simple Fun #112: Array Erasing

36 of 95myjinxin2015

Description:

Task

You are given an sequence of zeros and ones. With each operation you are allowed to remove consecutive equal elements, however you may only remove single elements if no more groups of consective elements remain. How many operations will it take to remove all of the elements from the given sequence?

Example

For arr = [0, 1, 1, 1, 0], the result should be 2.

It can be cleared in two steps:

[0, 1, 1, 1, 0] -> [0, 0] -> [].

For arr = [0, 1, 0, 0, 0], the result should be 3.

It can be cleared in three steps:

[0, 1, 0, 0, 0] -> [0, 1] -> [0] -> []

Note that you can not remove 1 at the first step, because you cannot remove just one element while there are still groups of consective elements (see the rule above ^_^)

Input

An array arr of 0s and 1s.
1 <= arr.length <= 100

Output

The minimum number (integer) of operations.

Special thanks:

Thanks for docgunthrop's solution ;-)

Algorithms

Stats:

CreatedFeb 10, 2017
PublishedFeb 10, 2017
Warriors Trained1114
Total Skips27
Total Code Submissions2334
Total Times Completed95
JavaScript Completions36
Python Completions63
Total Stars44
% of votes with a positive feedback rating93% of 28
Total "Very Satisfied" Votes24
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes0
Total Rank Assessments5
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • mauro-1 Avatar
  • Kacarott Avatar
Ad