7 kyu

Simple Fun #124: Lamps

269 of 604myjinxin2015

Description:

Task

N lamps are placed in a line, some are switched on and some are off. What is the smallest number of lamps that need to be switched so that on and off lamps will alternate with each other?

You are given an array a of zeros and ones - 1 mean switched-on lamp and 0 means switched-off.

Your task is to find the smallest number of lamps that need to be switched.

Example

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

a --> 1 0 0 1 1 1 0 swith --> 0 1 0 became--> 0 1 0 1 0 1 0

Input/Output

  • [input] integer array a

array of zeros and ones - initial lamp setup, 1 mean switched-on lamp and 0 means switched-off.

2 < a.length <= 1000

  • [output] an integer

minimum number of switches.

Puzzles

Stats:

CreatedFeb 15, 2017
PublishedFeb 15, 2017
Warriors Trained1205
Total Skips31
Total Code Submissions2274
Total Times Completed604
JavaScript Completions198
C# Completions82
Python Completions269
PHP Completions71
Ruby Completions41
Total Stars20
% of votes with a positive feedback rating94% of 175
Total "Very Satisfied" Votes158
Total "Somewhat Satisfied" Votes13
Total "Not Satisfied" Votes4
Total Rank Assessments6
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • hobovsky Avatar
  • Haksell Avatar
  • dfhwze Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad