[Kenchon book to Python]-Chapter 2- "Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!

Introduction

This article is Kencho-san's book, which contains many explanations of competitive programming, ** Train your problem-solving skills! Algorithms and data structures(けんちょんさん本)**について、掲載コードをPythonに翻訳したものを、備忘のためまとめたものです。

On this page, we will introduce the contents of Chapter 2! Please forgive me if there are any bugs.

See the following pages for links to other chapters. [Table of Contents Page] https://qiita.com/KevinST/items/002676619a583754bf76

code2.1 Computational complexity of single for statement

It's a problem that shows the amount of calculation of a single for statement. I also added the output.

code2-1.py


#Receive N from input
N = int(input())
count = 0

for i in range(N):
    count += 1

#Output result
print(count)

[Input example] 100 [Output example] 100

So-called $ O (N) $!

code2.2 Computational complexity of double for statement

This time it is the amount of calculation of the double for statement

code2-2.py


N = int(input())
count = 0
for i in range(N):
    for j in range(N):
        count += 1
print(count)

[Input example] 100 [Output example] 10000

So-called computational complexity $ O (N ^ 2 $)!

code2.3 Even enumeration

code2-3.py


N = int(input())
for i in range(N, 1, -2):
    print(i)

[Input example] 10 [Output example] 10 8 6 4 2

The amount of calculation is $ O (N) $.

code2.4 All enumeration for recent point pairs

code2-4.py


def calc_dist(x1, y1, x2, y2):
    return ((x1 - x2)**2 + (y1 - y2)**2)**0.5

#Receive input data
N = int(input())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int, input().split())

#Initialize the desired value with a sufficiently large value
minimum_dist = 10000000.0

#Start exploration
for i in range(N):
    for j in range(i+1, N):
        dist_i_j = calc_dist(x[i], y[i], x[j], y[j])
        if dist_i_j < minimum_dist:
            minimum_dist = dist_i_j

print(minimum_dist)

[Input example] 4 1 1 2 2 12 66 18 31 [Output example] 1.4142135623730951

In the above example, --Recent point pairs are (1,1) (2,2) --The distance is $ \ sqrt2 $ --Computational complexity is $ O (N ^ 2) $ is.

Click here for Chapter 3 https://qiita.com/KevinST/items/4d04dc7369880670a63b

Recommended Posts

[Kenchon book to Python]-Chapter 2- "Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
[Kenchon book to Python]-Chapter 4-"Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
[Kenchon book to Python] "Train your problem-solving skills! Algorithms and data structures" I tried to rewrite the posted code in Python! -table of contents-
"Book to train programming skills to fight in the world" Python code answer example --1.3 URLify
"Book to train programming skills to fight in the world" Python code answer example --2.6 palindrome
"Book to train programming skills to fight in the world" Python code answer example --2.4 Splitting the list
"Book to train programming skills to fight in the world" Python code answer example --2.7 intersecting nodes
"A book to train programming skills to fight in the world" Python code answer example --1.8 "0" matrix
"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression
"A book to train programming skills to fight in the world" Python code answer example --3.1 Three stacks
"A book to train programming skills to fight in the world" Python code Solution example --1.7 Matrix rotation
"Book to train programming skills to fight in the world" Python code answer example --1.4 Permutation of sentences
"A book to train programming skills to fight in the world" Python code Solution example --2.8 Loop detection
"Book to train programming skills to fight in the world" Python code solution example --- Removed elements between 2.3
"Book to train programming skills to fight in the world" Python code Solution example --2.1 Remove duplicate elements
"A book to train programming skills to fight in the world" Python code answer example --1.9 Rotation of strings
"A book to train programming skills to fight in the world" Python code answer example --1.1 Duplicate character string
"A book to train programming skills to fight in the world" Python code answer example --1.2 Count the number of the same characters
I felt that I ported the Python code to C ++ 98.
"A book to train programming skills to fight in the world" Python code Solution example --2.5 The sum of two numbers shown in the list
Solve the spiral book (algorithm and data structure) with python!
Build a Python environment and transfer data to the server
I want to know the features of Python and pip
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
I tried to solve the ant book beginner's edition with python
[Introduction to Python] I compared the naming conventions of C # and Python.
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
I wrote the code to write the code of Brainf * ck in python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
I tried to refactor the template code posted in "Getting images from Flickr API with Python" (Part 2)