Solve the spiral book (algorithm and data structure) with python!

* This article will be updated from time to time.

What article?

This article solves the problem of spiral books. The language is python. I also wrote a bite memo, so please refer to it.

chapter2

Maximum Profit

n = int(input())
price = []
for k in range(n):
    price.append(int(input()))

min_price = 1000000001
max_pro = -200000

for i in range(n):
    max_pro = max(max_pro, price[i]-min_price)
    min_price = min(price[i], min_price)

print(max_pro)

Chapter 11 (Dynamic Programming)

11.4 Chain matrix product

First, it is difficult to formulate a recurrence formula. Furthermore, it is difficult to think about the order of calculation. The order of calculation is [here](https://aotamasaki.hatenablog.com/entry/2019/11/03/%E8%9E%BA%E6%97%8B%E6%9C%AC%E3%82 % 92 Python% E3% 81% A7% E8% A7% A3% E3% 81% 8F_Part2 # P257-ALDS1_10_B-Matrix-Chain-Multiplication).

n = int(input())
p = []
for t in range(n):
    a, b = map(int, input().split())
    if t == 0:
        p.append(a)
    p.append(b)

'''
dp[i][j]Is Mi~Minimum number of multiplications to calculate Mj
'''
dp = [[float('inf')] * (n+1) for j in range(n+1)]
for k in range(n+1):
    dp[k][k] = 0
    dp[0][k] = 0
    dp[k][0] = 0

#l is the distance from the diagonal component
for l in range(1,n+1):
    for i in range(0,n-l+1):
        j = i + l
        for k in range(0,j-i):
            dp[i][j] = min(dp[i][j], dp[i][i+k] + dp[i+k+1][j] + p[i-1] * p[j] * p[i+k])
print(dp[1][n])


'''
input
6
30 35
35 15
15 5
5 10
10 20
20 25

output
15125
'''

Recommended Posts

Solve the spiral book (algorithm and data structure) with python!
Picture book data structure algorithm Python
Try to solve the programming challenge book with python3
Solve the Python knapsack problem with a branch and bound method
Try to solve the shortest path with Python + NetworkX + social data
Data pipeline construction with Python and Luigi
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Python data structure and internal implementation ~ List ~
Search the maze with the python A * algorithm
Python data structure and operation (Python learning memo ③)
Algorithm learned with Python 11th: Tree structure
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Send and receive image data as JSON over the network with Python
Make a decision tree from 0 with Python and understand it (4. Data structure)
Solving the Lorenz 96 model with Julia and Python
Archive and compress the entire directory with python
Find the shortest path with the Python Dijkstra's algorithm
Solve "AtCoder version! Ant book (beginner)" with Python!
Solving the Python knapsack problem with the greedy algorithm
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Execute raw SQL using python data source with redash and display the result
Solve the Python asymmetric traveling salesman problem with a branch and bound method
Visualize railway line data and solve the shortest path problem (Python + Pandas + NetworkX)
Data analysis with python 2
Solve AtCoder 167 with python
Solve Sudoku with Python
[Python tutorial] Data structure
Solve POJ 2386 with python
Data analysis with Python
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
Algorithm learned with Python 18th: Sorting (stack and queue)
I tried to solve the soma cube with python
Visualize the range of interpolation and extrapolation with python
Solve simultaneous ordinary differential equations with Python and SymPy.
Get comments and subscribers with the YouTube Data API
Install the latest stable Python with pyenv (both 2 and 3)
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
Investigate Java and python data exchange with Apache Arrow
I tried to solve the problem with Python Vol.1
1st Algorithm Practical Test Solve past questions with python
Extract the band information of raster data with python
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
[Python] Solve equations with sympy
Sample data created with python
Programming with Python and Tkinter
Try scraping the data of COVID-19 in Tokyo with Python
Try hitting the Twitter API quickly and easily with Python
Encryption and decryption with Python
data structure python push pop
Solve AtCoder ABC166 with python
Python and hardware-Using RS232C with Python-
I wanted to solve the Panasonic Programming Contest 2020 with Python
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
Get Youtube data with python
[Python] Read the csv file and display the figure with matplotlib
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
Get rid of dirty data with Python and regular expressions
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
Return the image data with Flask of Python and draw it to the canvas element of HTML