Given a sequence of numbers (or array). Find the maximum dist

Source: Internet
Author: User

The following table describes the requirements for a given array column to find the maximum difference between the positions of the same data in the array, for example, 1, 2, 3, 4, 1, 1, 7, 4, max (1) = 5, max (2) = 0, max (4) = 4;

Two methods are provided. One is to use hash, which has limitations. First, if a value in the array is large, applying hash will be a waste of space, define such a data structure:

Typedef struct data_s {

Int value;

Int start;

Int end;

}

Set a hash array and traverse the array. record the position where the number appears for the first time and keep it unchanged. If the same number appears again, update the end in the data structure, after the array is traversed, the location where all numbers appear for the first time and the location where they appear for the last time are recorded. The time complexity and space complexity of the application are O (N ), however, this method has a large limitation, that is, the space loss, and the inability to determine the amount of space to be allocated. Since we cannot statically allocate a certain amount of space to record this information, we can dynamically allocate and apply the binary search tree to meet this requirement. However, the space complexity and time complexity are a bit high. The time complexity is O (n * logn), and the space complexity is O (n ). However, this method is much better than hash. It is a good choice to apply the binary search tree without requiring a quick solution to the problem. The following code is provided. If there are any errors, please note:


[Cpp]
# Include <iostream>
Using namespace std;
 
Typedef struct data_s {
Int value;
Int start;
Int end;
} Data_t;
 
Typedef struct tree_node_s {
Data_t data;
Struct tree_node_s * lchild;
Struct tree_node_s * rchild;
} Tree_node_t, * BSTree;
 
Int tree_search (BSTree T, int value, tree_node_t ** p, tree_node_t * f ){
If (NULL = T ){
* P = f;
Return 0;
}
If (value = T-> data. value ){
* P = T;
Return 1;
} Else if (value <T-> data. value ){
Return tree_search (T-> lchild, value, p, T );
} Else {
Return tree_search (T-> rchild, value, p, T );
}
}
 
Void tree_insert (BSTree * T, int value, int index ){
Tree_node_t * p = NULL;
If (! Tree_search (* T, value, & p, NULL )){
Tree_node_t * temp = (tree_node_t *) malloc (sizeof (tree_node_t ));
Temp-> data. value = value;
Temp-> data. start = index;
Temp-> data. end = index;
Temp-> lchild = NULL;
Temp-> rchild = NULL;
If (NULL = (* T )){
* T = temp;
} Else if (value <p-> data. value ){
P-> lchild = temp;
} Else {
P-> rchild = temp;
}
} Else {
P-> data. end = index;
}
}
 
Void tree_traverse (BSTree T ){
If (T ){
Tree_traverse (T-> lchild );
Cout <"value:" <T-> data. value <"start at:" <T-> data. start <
"End at:" <T-> data. end <"distance:" <T-> data. end-T-> data. start <endl;
Tree_traverse (T-> rchild );
}
}
 
Void tree_destroy (BSTree * T ){
If (* T ){
Tree_destroy (& (* T)-> lchild );
Tree_destroy (& (* T)-> rchild );
Free (* T ));
}
}
 
Int main (int argc, char * argv []) {
Int I;
BSTree T = NULL;
Int arr [] = {1, 2, 3, 4, 1, 1, 7, 4 };
Int len = sizeof (arr)/sizeof (int );
For (I = 0; I <len; I ++ ){
Tree_insert (& T, arr [I], I );
}
Tree_traverse (T );
Tree_destroy (& T );
Cin. get ();
Return 0;
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.