The K-largest number in the array can be obtained based on the fast sorting method. The steps are as follows:
1. Randomly select a fulcrum
2. Place the number greater than the pivot point to the left of the array. Place the number smaller than the pivot point to the right of the array. Place the pivot point to the center (left)
3. Set the length of the left part to L,
When K When K> L, find the number (K-L) in the part recursively
When K = L, returns the Split points (that is, the fulcrum of the
The number of K-large in the array can be based on the idea of fast ordering, as follows:
1. Select a fulcrum at random2, will be larger than the fulcrum of the number, put to the left of the array, the number of smaller than the fulcrum to the right side of the array, put the fulcrum in the middle (belong to the left part)3, set the length of the left part is L,When K When K > L, recursively find the number of the first (K-L) in the section
When k = L, return the left and right parts of the sp
Question: There is an unordered positive integer group with a 2n number of elements. Required: How can I divide this array into two arrays with N numbers and make the two subarrays closer to the nearest one?
1 1 2-> 1 1 vs 2
Generally, the time complexity of the solution is greater than or equal to O (N ^ 2 ). A sudden burst of inspiration, and a new solution, should be considered a dynamic planning process
The definition of a return string: "Return string" is a string with the same reading and reverse reading. For example, "level" or "Noon" is a return string. A substring, as the name implies, that is, a substring in a string that meets the background nature.
Some questions are often discussed around the reply substrings. For example, poj1_4 is the longest reply string and the length of the longest reply string is obtained. The simple algorithm scales each character in turn to both sides. Obvious
the time complexity of recursive algorithms is not linear unless there are only the first two items, and memory consumption. We use the most common Fibonacci series to describe:
FunctionFibonacci (n ){If(N = 0 | n = 1){ReturnN ;}Else{ReturnFiber-ACCI (n-1) + fiber-ACCI (n-2);}}
This is the most common writing method, which is extremely memory-consuming. When the parameter n is greater than 30, it will
number. The expected complexity of the queue is O (Nlogn), because the fast row will be recursively processed on both sides of the partition, while the K large number only needs to deal with the dividing side, and its expected complexity will be O (n). See "Introduction to Algorithms" for detailed proof.We can think of this roughly:Assuming our data is random enough, each division is in the middle of the d
PrefaceGenerally, for a given algorithm, we need to do
:If F (n) is set to algorithm A in the worst case of F (N), if F (n) belongs to algorithm (G (n )), then, the maximum time complexity of algorithm A is g (n), and g (n) is F (n.If F (n) is set to algorithm A in the worst case of F (N), if F (n) belongs to Ω (G (n )), then, the lower limit of the approximate time
Ordered single-chain table to BST tree + time complexity requirements O (n), single-chain bstProblem description
There are usually multiple methods to convert an ordered array or linked list into a relatively balanced BST tree.1. when an ordered array A is converted into a bst, convert the element A [m] in the middle of the array to the root, and then recursively convert it to A [1 .. m-1], A [m + 1 .. n.
Inverse pair definition: Set A[1..N] is an array that contains n different numbers. If, in the case of IAn algorithm is given to determine the number of inverse pairs in any permutation of n elements using the worst-case run time of O (N*LGN).Simple Algorithm Realization idea: we can simply through the previous comparison to determine the number of reverse pairs, although the implementation is simple, but this tim
In algorithm analysis, when an algorithm includes recursive calls, the analysis of its time complexity is converted into a recursive equation. In fact, this problem is a mathematical solution to the gradual order, and the recursive equation is in a variety of forms, the solution method is also not enough, than the limit is often used in the following four methods:
(1) Substitution Method)The basic step of t
Tonight did a company's network pen test, long time no brush problem, now slag to die, inside Youdao Program design problem is to put an array to the right of the circular move K-bit requirements time complexity O (n) to the method defined as Public void solution (int a[],int length,int k)I thought it was easy, the results of a write to find only one bit ... Publ
In algorithm analysis, when a recursive call is included in an algorithm, the analysis of its time complexity is transformed into a recursive equation solution. In fact, this problem is a mathematical solution to the asymptotic order of the problem, and the recursive equation of the form of various, the solution is also many, the more often used in the following four ways:(1) Substituting law (Substitution
Transfer From address http://blog.csdn.net/metasearch/article/details/4428865In algorithm analysis, when a recursive call is included in an algorithm, the analysis of its time complexity is transformed into a recursive equation solution. In fact, this problem is a mathematical solution to the asymptotic order of the problem, and the recursive equation of the form of various, the solution is also a variety o
Introduction: the solution of time complexity is explained here by examples, which can be understood by readers. All of the following cases are written in Python!
Case 1: Calculate the Npower of
The Code is as follows:
Def exp1 (A, n ):
If n = 1:
Return
Else:
Return a * exp2 (A, n-1)
Analysis: 1. The problem scale is N; 2. When the scale is 1, it ends; 3. Suppose T (n) represents the number of steps require
1. Brief Introduction
First, we need to decide whether to implement the chain table structure or the ordered structure. For the implementation of the ordered structure, when the data is full, we need to open up new Arrays for replication, therefore, the time complexity of push cannot be O (1). the linked list structure ensures that the time
created. If ArrayType is a reference type, a reference to the Allocationsize arraytype type will be created. If we assign values to some locations in the fileinfo[] array, the reference relationship is shown.Arrays in. NET support Direct read and write operations on elements. The syntax is as follows:1//reading group element 2 bool B = booleanarray[7];3 4//write array element 5 booleanarray[0] = false;The time co
Problem: time complexity of the tower-based Recursive Algorithm
The algorithm is as follows:
Description: size indicates the scale of the tower. startstack indicates the starting position of the tower, endstack indicates the completion, and midstack indicates the secondary node.
Def towers (size, startstack, endstack, midstack ):
If size = 1:
Print 'move disk from', firststack, 'to', endstack
Else:
Towers (
1. Description of the problemGiven a single linked list, it is judged that the content is not a palindrome type. such as 1–>2–>3–>2–>1. The time and space complexity are as low as possible.2. Methods and Ideas1) relatively simple algorithm.Since the given data structure is a single-linked list, to access the trailing elements of the linked list, you must traverse from the beginning. For the convenience of j
The main element of a data series is the element in the sequence that occurs more than half the length of the sequence.Method 1 (Expected time complexity is O (n)):Because the main element occurs more than half the length of the sequence, the main element must be the median. Recursive partitioning can be used to find the median, and the expected time
Problem Description:Find the longest palindrome substring in a stringViolence law: Fixed Center, the method of extending from 0 length to both ends O (n^2), n >= 10^5 or timeout, so only O (n^2)Manacher ' s algorithm: Fixed center, from P[r], (can be determined with this point as the center ends of the palindrome is the length of the beginning), extending to both ends, time complexity, O (n)The algorithm is
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.