TSS Contest P1 - Over and Under

View as PDF

Submit solution

Points: 3
Time limit: 2.0s
Java 8 3.0s
Memory limit: 256M

Author:
Problem type

Prom is coming up and you want to ask your high school crush to be your date to the event. To do this, you decide to sew them a red and green scarf as a present. Sewing, as it turns out, is not as easy as it may seem. You have an array of red and green threads lined up and you want to run a black thread perpendicular to it, going over each red thread and under each green thread like this:

threads

Though seemingly a simple task you find after the job is done that sometimes your black thread goes over the green and sometimes under the red. Luckily, you have found a way to magically force the thread under or over at any position that you wish. Given the arrangement of the black thread, determine the minimum number of times you need to change the position of the thread.

Input Specification

The first line of input contains n (1 \le n \le 10^6) representing the number of red and green threads. The scarf will always begin with a red thread and alternate between red and green threads. When sewn correctly, the black thread should go over every red thread and under every green thread.
The next line contains n space separated words, each one is either over or under representing that the ith red or green thread has the black thread going over or under it.

Output Specification

Output an integer representing the number of times you must change the position of the black thread in order for the scarf to be sewn correctly.

Sample Input

7
over under over over under over over

Sample Output

3

Comments

There are no comments at the moment.