Chapter 6 Introduction to algorithms Question 6-3 young Matrix

Source: Internet
Author: User

This problem uses the binary heap to maintain the heap nature to maintain the nature of the young matrix. The question prompt is clearly written, but it is really easy to bend.

A and B are simple. Let's look at C:

According to the nature of the Young's matrix, the minimum value must be obtained in the upper left corner. The problem is how to maintain the nature of the matrix after the minimum value is obtained. You can refer to the practice in max_heapify to obtain the minimum value first, and then set the upper left corner of the Matrix to the maximum value, so that the elements in the upper left corner will inevitably lead to the nature of the young matrix, so consider the elements on the right of the element and the elements below the element. The question is, should we exchange the elements on the right or the elements below? We can find that if we exchange a small element with T (right) and T (bottom), we can restore the row or column where the other element is located to the young matrix, in this way, the size of the matrix to be adjusted is reduced to (m-1) x n or m x (n-1 ). Recursively run this process until the matrix to be adjusted is empty, because M or N is reduced by 1 each time until they are all reduced to zero, the time complexity is O (m + n)

 

D. Ask whether to implement the Insert Process. Refer to the Insert Process in the binary heap to insert the new element to the end of the matrix, and then adjust it to the upper left corner until it reaches the appropriate position. The idea of the adjustment process is similar to that of C. This is a comparison with the element on the left of the new element and the element above. It can be found that if I exchange the new element with the larger element T (left) and T (top), the row (or column) of the other element can be) restores the young matrix, so the size of the matrix to be adjusted is also reduced to (m-1) x n or m x (n-1)

 

E is relatively direct. extract_min requires linear time O (N). There are n ^ 2 elements, so the complexity is O (n ^ 3)

 

F is difficult to think about. I have been tossing for a long time. The key is to find the position starting from the lower left corner of the matrix. This position is clever. If K is greater than it, the column where it is located does not need to be searched. If K is smaller than it, the row where it is located does not need to be searched, it is also an element for successively decreasing the size of the matrix.

The source code of some implementations is as follows:

#include<iostream>#include<vector>using namespace std;enum class Direction{DOWN, RIGHT, UP, LEFT, OVER};void extract_helper(vector<vector<int> > &Young, int m, int n){Direction d = Direction::OVER;int val = Young[m][n];if (m + 1 < Young.size() && val > Young[m + 1][n]){d = Direction::DOWN;val = Young[m + 1][n];}if (n + 1 < Young[0].size() && val > Young[m][n + 1]){d = Direction::RIGHT;val = Young[m][n + 1];}switch (d){case Direction::DOWN:swap(Young[m][n], Young[m + 1][n]);extract_helper(Young, m + 1, n);break;case Direction::RIGHT:swap(Young[m][n], Young[m][n + 1]);extract_helper(Young, m, n + 1);break;case Direction::OVER:break;    }return;}int extract_min(vector<vector<int>> &Young){int min = Young[0][0];Young[0][0] = INT_MAX;int m = 0;int n = 0;extract_helper(Young, m, n);return min;}void insert_helper(vector<vector<int>> &Young, int m, int n){ Direction d = Direction::OVER; int val = Young[m][n];if (m - 1 >= 0 && val < Young[m - 1][n]){val = Young[m - 1][n];d = Direction::UP;}if (n - 1 >= 0 && val < Young[m][n - 1]){val = Young[m][n - 1];d = Direction::LEFT;}switch (d){case Direction::UP:swap(Young[m][n], Young[m - 1][n]);insert_helper(Young, m - 1, n);break;case Direction::LEFT:swap(Young[m][n], Young[m][n - 1]);insert_helper(Young, m, n - 1);break;case Direction::OVER:break;}}void insert(vector<vector<int>> &Young, int key){int M = Young.size();int N = Young[0].size();Young[M - 1][N - 1] = key;int m = M - 1;int n = N - 1;insert_helper(Young, m, n);}void Young_sort(vector<vector<int>> &Young, vector<int>& result){while (Young[0][0] != INT_MAX){int key = extract_min(Young);result.push_back(key);}}

  

Chapter 6 Introduction to algorithms Question 6-3 young Matrix

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.