Java programming array maxchild matrix simple solution implementation code, java Array
This article focuses on the content of the largest child matrix in the Java programming array. The details are as follows.
If you meet a good person, you can change your life. If you encounter a good book, why not?
I recently read from Mr. Zuo Chengyun's《Programmer code interview guide-IT name Enterprise algorithm and data structure question Optimal Solution"It is very insightful. It is recommended that you also observe those bloggers who are interested in this aspect.
The algorithm for the maximum matrix of stack-based arrays described in the book is classic, but the bloggers have limited capabilities and fail to fully understand the essence of the algorithm. However, according to this idea, the blogger has come up with a simple algorithm to address this type of problem. The following is an overview.
Core Ideology
Let's take a look at a picture and we can get a rough understanding of it.
Every round is an operation, and our core is an internal operation for each round.
Calculate the maximum continuous length of each layer.
That is to say, we calculate the bottom-to-bottom round of the array, and then calculate the area of the continuous maximum submatrix that can be obtained in this round for each round. Then, you only need to compare the maximum data of each round, and then return the result to find the maximum continuous child matrix area of the array.
Code
Well, with the core idea above, we can start to write code. (Although I do not think it is clear, sorry ).
Package stack_and_queue; /*** @ author Guo Pu <br> * calculate the area of the largest consecutive rectangle area based on the array */public class MaxRectangle {public static void main (String [] args) {Integer [] arr = {2, 1, 3, 5, 7, 6, 4}; Integer maxRectangle = maxRectangleArea (arr); System. out. println ("the maximum continuous area of the rectangle in the array is:" + maxRectangle );} /*** @ param arr * @ return maximum area of the continuous rectangular area in the array */private static Integer maxRectangleArea (Integer [] arr) {int [] result = new I Nt [arr. length]; // calculates the traversal of the array, from the bottom up continuous length for (int I = 1; I <= arr. length; I ++) {// The temporary value that accumulates the continuous length in the current round. int templen = 0; // record the maximum continuous length int templen_max = 0 at the current height; // The inner loop should start from the array header, starting with the subscript of the first layer, the previous data may be lost. for (int j = 1; j <= arr. length; j ++) {if (arr [j-1]> = I) {templen + = 1; templen_max = templen;} else {templen = 0 ;}} result [I-1] = I * templen_max; // System. out. println (the maximum continuous length of the "+ I +" layer is: "+ tem Plen_max);} // obtain the maximum number of values in the result set, that is, the area of the maximum rectangular field in the continuous area int maxArea = 0; for (int I = 0; I <result. length; I ++) {maxArea = maxArea> result [I]? MaxArea: result [I] ;}// return the return maxArea of the obtained maximum continuous rectangle ;}}
The comments in the Code are also comprehensive, so we will not repeat them too much.
Test
Next we will test the array. First, let's test the array shown in the image starting with this article.
Integer[] arr = {2,1,3,5,7,6,4}···
The area of the largest continuous rectangular area in the array is: 16
Then, let's modify the element values in the array to further test whether the results are correct.
Integer[] arr = {2,1,3,1,7,6,4}···
The area of the largest continuous rectangular area in the array is 12.
The author personally tests the algorithm and works properly. :)
Optimization
When talking about the optimization part, we can see that the estimation is the final step to find the maximum value in the number group of result sets.
Indeed, we can apply for another variable to record the area of the largest submatrix of rounds so far. However, this optimization is not very effective, and the time complexity has not been greatly improved.
In addition, I think a better starting point is to add a judgment when performing computation on each round to determine whether or not to loop down before the current round. If the elements in the array fluctuate greatly, the optimization is still very good.
Summary
This small algorithm is more sophisticated. The only drawback is that the time complexity is a little greater. If you are looking for an algorithm with a relatively low time complexity, take a detour.
If you only want to seek a result, it is good. It is at least much more efficient than brute-force computing.
The above is all the content of the Code about the simple solution of the Maximum child matrix in the Java programming array, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!