Simple Fun #112: Array Erasing
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 ;-)
Similar Kata:
Stats:
Created | Feb 10, 2017 |
Published | Feb 10, 2017 |
Warriors Trained | 1114 |
Total Skips | 27 |
Total Code Submissions | 2334 |
Total Times Completed | 95 |
JavaScript Completions | 36 |
Python Completions | 63 |
Total Stars | 44 |
% of votes with a positive feedback rating | 93% of 28 |
Total "Very Satisfied" Votes | 24 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 5 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 5 kyu |