[PYTHON] Competitive programming is what (bonus)

This article is an extra article of Competitive Programming is What.

If you implement it, it will be an article with the source code of.

To make it easier for people who are not doing python, There are some parts that are redundant code (for competition pros), but please forgive me.

Simple solution

# coding: utf-8
#Magic for python that speeds up standard input acquisition
import sys
input = sys.stdin.readline

#Get while converting standard input numerically
N = int(input())
#Get standard input while converting array & number
H = [int(x) for x in input().split()]

#Calculate the maximum distance that can be moved when landing on each square
#Definition of variables for storing the travelable distance
ans = []

#Shift the start position from the beginning
for i in range(N):
    count = 0
    #Check how much you can move from the start position
    for j in range(i,N-1):
        if H[j] >= H[j+1]:
            #If you can move, increase the distance you can move
            count += 1
        else:
            #End if not moveable
            break
    #Add to the list of travelable distances
    ans.append(count)

#Displaying answers
print(max(ans))

Optimal solution

# coding: utf-8
#Magic for python that speeds up standard input acquisition
import sys
input = sys.stdin.readline

#Get while converting standard input numerically
N = int(input())
#Get standard input while converting array & number
H = [int(x) for x in input().split()]
#So far together

#Processing-like variable definition
max_count = 0
tmp_count = 0

#Start from the beginning
for i in range(N-1):
    if H[i] >= H[i+1]:
        #Increase the distance count if you can move
        tmp_count += 1
    else:
        #When it is not possible to move, the distance that can be moved up to that point
        #Compare current maximum travel distance
        if tmp_count > max_count:
            #Since the movable distance this time is larger, I will update it
            max_count = tmp_count
        #Reset the current travel distance
        tmp_count = 0

#The loop end is N-Since it is 1, if you can move from a certain point to the right end,
#Since the distance traveled has not been compared to the maximum distance traveled,
#Use max and use the larger value as the answer
print(max(max_count,tmp_count))

Recommended Posts

Competitive programming is what (bonus)
What is namespace
What is copy.copy ()
What is Django? .. ..
What is dotenv?
What is POSIX?
What kind of programming language is Python?
What is klass?
What is SALOME?
What is Linux?
What is python
What is hyperopt?
What is Linux
What is pyvenv
What is __call__
What is Linux
What is Python
What is "functional programming" and "object-oriented" in Python?
What is a distribution?
What is Piotroski's F-Score?
I tried competitive programming
What is Raspberry Pi?
[Python] What is Pipeline ...
What is Calmar Ratio?
What is a terminal?
[PyTorch Tutorial ①] What is PyTorch?
What is hyperparameter tuning?
Competitive programming diary python 20201213
What is a hacker?
Competitive programming diary python 20201220
What is JSON? .. [Note]
Competitive programming with python
What is Linux for?
What is a pointer?
What is ensemble learning?
What is TCP / IP?
What is Python's __init__.py?
What is an iterator?
Competitive programming diary python
What is UNIT-V Linux?
[Python] What is virtualenv
What is machine learning?
What is Minisum or Minimax?
What is Linux? [Command list]
What is Logistic Regression Analysis?
What is the activation function?
What is the Linux kernel?
What is an instance variable?
What is a decision tree?
What is a Context Switch?
What is Google Cloud Dataflow?
[DL] What is weight decay?
[Python] Python and security-① What is Python?
What is a super user?
Python Competitive Programming Site Summary
[Python] * args ** What is kwrgs?
What is a system call
Story of trying competitive programming 2
[Definition] What is a framework?
What is the interface for ...
What is Project Euler 3 Acceleration?