


Maximum subsequence sum algorithm analysis, subsequence algorithm analysis_PHP tutorial
Maximum subsequence sum algorithm analysis, subsequence algorithm analysis
Problem description: Given n integer sequences {a1, a2,...,an}, find the function f (i,j)=max{0,Σak}(k: continuously taken from i to j);
The problem is to find the maximum value of the sum of consecutive sub-columns. If the maximum value is a negative number, take 0, such as an 8-number sequence {-1,2,-3,4,-2,5, -8,3}, the maximum subsequence sum of Namo is 4 (-2) 5=7.
This problem has four algorithms with different complexity. The time complexity of algorithms 1 to 4 is O(n3), O(n2), O(nlogn ),O(n);
Algorithm 1:
The most direct method is the exhaustive method. List all situations. We can set the left end i and right end j of the subsequence, and then use one layer to calculate the sum from a[i] to a[j].
//Maximum subcolumn and exhaustive method
#include
using namespace std;
int Find_Maxsun(int*a, int n);
int main(){
int n, i;
int a[100];
cin >> n;
cout for (i = 0; i cin >> a[i];
cout return 0;
}
int Find_Maxsun(int*a, int n){
int MaxSun = 0, i, j, k;
int NowSum;
for (i = 0; i for (j = 0; j NowSum = 0;
for (k = i; k NowSum = a[k]; /*Subsequence from a[i] to a[j]*/
if (NowSum>MaxSun)
MaxSun = NowSum; /*Update results*/
}
return MaxSun;
}
Obviously, the brute force method uses three for loops, and the algorithm time complexity is O(n3). This is of course the stupidest algorithm, but when the data is very large, even if it is To calculate the rhythm until death, we can clearly see the third layer of for loop,
Every time j is added, the sum of the subseries must be calculated again, so why don’t we use the result of j-1? That is to say, we save the result of j-1. When calculating the result of step j, we only need to add a[j] on the basis of step j-1, so there is Algorithm 2.
Algorithm 2:
#include
using namespace std;
int Find_Maxsun2(int*a, int n);
int main(){
int n, i;
int a [100];
cin >> n;
cout for (i = 0; i cin >> a[i];
cout return 0 ;
}
int Find_Maxsun2(int*a, int n){
int i, j, NewSum = 0, MaxSum= 0;
for (i = 0; i NewSum = 0;
for (j = i; j NewSum = a [j]; /*Update NewSum each time under j-1 condition*/
if (NewSum>MaxSum) /*Update MaxSum*/
MaxSum = NewSum;
}
}
return MaxSum;
}
This algorithm is smarter than 1, and the algorithm complexity is O(n2), which is obviously not the complexity we want.
Algorithm 3:
Algorithm 3 uses the idea of divide and conquer. The basic idea is self-evident: first divide and then conquer, decompose the problem into small problems and then sum up the small problems to solve. We divide the original sequence into two, then The largest subsequence is on the left, on the right, or across the boundary. The basic idea is as follows:
Step one: Divide the original sequence into two, into a left sequence and a right sequence.
Step 2: Recursively find the subsequences S left and S right.
Part 3: Scan from the center line to both sides to find the largest subsequence across the center line and S.
Step 4: Find S=max{S left, S middle, S right};
The code is implemented as follows:
#include
using namespace std;
int Find_MaxSum3(int*a,int low,int high);
int Max(int a,int b,int c);
int main(){
int n, i;
int a[100];
cin >> n;
cout for (i = 0; i cin >> a[i];
cout return 0;
}
int Find_MaxSum3(int*a,int low,int high){
int MaxSum = 0, MidSum, LeftSum, RightSum,i;
MidSum = 0;
if (low == high){ /*Termination condition of recursion*/
if (a[low] > 0)
return a[low];
else
return 0;
}
int mid = (low high) / 2; //Find the midpoint of the score
LeftSum = Find_MaxSum3( a, low, mid); /*Recursively find the maximum sum of the left sequence*/
RightSum = Find_MaxSum3(a, mid 1, high); /*Recursively find the maximum subsequence sum of the right sequence*/
/*Then You can find the maximum sum of the intermediate crossing-border sequences */
int NewLeft = 0,Max_BorderLeft=0, NewRight = 0,Max_BorderRight=0;
for (i = mid; i >= low; i--) { /*Scan left to find the maximum sum*/
NewLeft = a[i];
if (NewLeft > Max_BorderLeft)
Max_BorderLeft = NewLeft;
}
for (i = mid 1; i NewRight =a[i];
if (NewRight >= Max_BorderRight)
Max_BorderRight = NewRight ;
}
MidSum = Max_BorderRight Max_BorderLeft;
return Max(LeftSum, MidSum, RightSum); /*Return the result of treatment*/
}
int Max(int a, int b, int c){ /*Find the largest number among the 3*/
if ( a>= b&&a >= c)
return a;
if (b >= a&&b >= c )
return b;
if (c >= b&&c>=a)
return c;
}
Let’s calculate the time complexity of this algorithm:
T(1)=1;
T(n)=2T(n/2) O(n);
=2kT(n/2k) kO(n)=2kT(1) kO(n) (where n= 2k)=n nlogn=O(nlogn);
Although this algorithm is very good, it is not the fastest algorithm.
Algorithm 4:
Algorithm 4 is called online processing. This means that every time a piece of data is read in, it is processed in time, and the result obtained is true for the currently read data, that is, the algorithm can give the correct solution at any position, and the algorithm can give the correct solution while reading.
#include
using namespace std;
int Find_MaxSum4(int*a, int n);
int main(){
int n, i;
int a [100];
cin >> n;
cout for (i = 0; i cin >> a[i];
cout return 0 ;
}
int Find_MaxSum4(int*a, int n){
int i, NewSum = 0, MaxSum = 0;
for (i = 0; i NewSum = a[i]; /*Current subsequence sum*/
if (MaxSum MaxSum = NewSum; /*Update maximum subsequence sum*/
if (NewSum NewSum = 0;
}
return MaxSum;
}
This algorithm scans the read data one by one, with only one for loop. The algorithms for solving the same problem are very different. The trick is to let the computer remember some key intermediate results to avoid repeated calculations.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools