#🔒 Solve this exercise, I can never get it to work

6 messages · Page 1 of 1 (latest)

limpid orbit
#

Builder

In a street, there are N adjacent buildings, the i-th building (from left to right) has (a_i) floors. A builder has come up with a way to adjust the heights of the buildings so that they all have the same number of floors, by performing a series of phases.

According to the rules defined by the Brazilian Society of Construction (SBC), each time the builder increases a building's height, all adjacent buildings that have the same number of floors should also be increased by 1 floor.

For example, suppose (N) is equal to 8, where the initial heights are (a_1 = 5, a_2 = 4, a_3 = 4, a_4 = 4, a_5 = 5, a_6 = 4, a_7 = 4, a_8 = 7).

  • The builder can define (L = 2) and (R = 3) for the first phase, so that all heights at the end will be: (5, 4, 4, 4, 6, 4, 4, 7).
  • The builder can then define (L = 2) and (R = 4) for the second phase, so that the heights will finally be: (5, 5, 5, 5, 6, 4, 4, 7).

To complete the work, the builder wants to minimize the number of phases needed. In the example mentioned, how can you achieve this?

The goal is to determine the minimum number of phases required to complete the construction.

Input

The first line of the input contains a single integer (N) (the number of buildings in the street).
The second line contains (N) integers, where the i-th integer (a_i) is the number of floors of the i-th building.

Output

The output should contain a single integer, the minimum number of phases needed to complete the work.

Constraints

  • (2 \leq N \leq 100)
  • (1 \leq a_i \leq 100)

Examples

Example Input 1

4
3 1 1 2

Example Output 1

2

Example Input 2

8
5 4 4 4 5 4 4 7

Example Output 2

4

Example Input 3

3
100 100 100

Example Output 3

0


loud spruceBOT
#

@limpid orbit

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

limpid orbit
#
def contar_fases(alturas):
    fases = 1  # Começamos com uma fase inicial
    n = len(alturas)

    for i in range(1, n):
        # Sempre que a altura atual for menor que a anterior, precisamos de uma nova fase
        if alturas[i] < alturas[i - 1]:
            fases += 1

    return fases

# Leitura da entrada
N = int(input())  # Número de prédios
alturas = list(map(int, input().split()))  # Altura de cada prédio

# Processamento
resultado = contar_fases(alturas)

# Saída
print(resultado)

#

Input:
8
5 4 4 4 5 4 4 7
Expected output:
4
Received output:
3

loud spruceBOT
#

@limpid orbit

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.