Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

left right list python task

$
0
0

Here is the task I was trying to solve

Denis is a true researcher whose days at the university are filled with lectures, experiments, and reports. He works diligently, but, like all students, sometimes makes mistakes and receives not the highest grades.Today, Denis faces a special challenge - he needs to provide his research supervisor with his reports. The professor asked to see all his works for some consecutive 7 days. Grades are represented as a sequence of integers from 2 to 5 inclusive - one grade for each day. Denis wants to choose such a continuous period of his reports that there are no works with grades 2 and 3 in this period, and the number of works with grade 5 is maximum.Help Denis find this special moment when his scientific light prevails over the darkness, and his works shine brightest!

Input formatThe first line contains a single natural number n - the number of reports (1 ≤ n ≤ 10^5). The second line contains integers - one grade for each day (2 ≤ m ≤ 5).

Output formatOutput the number of excellent reports in the selected period chosen by Denis, satisfying all conditions. If such a period does not exist, output -1.

I wrote this code and I don't understand what the problem is. I will be grateful for any help. And if you also give me an example of the code, then I will be happy. Thank you all in advance.

n = int(input())grades = list(map(int, input().split()))left = 0max_fives = 0current_fives = 0while left < n and (grades[left] == 2 or grades[left] == 3):    left += 1for right in range(left, n):    if grades[right] == 5:        current_fives += 1    while grades[right] == 2 or grades[right] == 3:        left += 1    max_fives = max(max_fives, current_fives)    if right - left >= 6:        if grades[left] == 5:            current_fives -= 1        left += 1if max_fives == 0:    print(-1)else:    print(max_fives)

There are 3 tests presented here

Input95 5 4 5 4 5 4 5 4Output4
Input83 4 4 4 4 5 4 5Output2
Input105 5 5 5 5 3 5 5 5 5Output-1

Everything breaks down on the 3rd check.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>