ljiatu@umich.edu
BAN USERThis will be expensive if data is some large structure or class. So the best way should be switching pointers.
- ljiatu@umich.edu January 06, 2013The code above only compares row[i] with column[i], which I don't think is correct.
- ljiatu@umich.edu December 29, 2012it says printing out those numbers who is the largest in its row and at the same time the smallest in its column, not largest ones in each row and smallest ones in each column
- ljiatu@umich.edu December 26, 2012I think it means that words in the string are separated by different numbers of spaces.
- ljiatu@umich.edu December 26, 2012C++
void find(int N, vector<vector<int> >& matrix)
{
vector<int> largestEachRow(N);
vector<int> smallestEachColumn(N);
for(int i = 0; i < N; i++) {
smallestEachColumn[i] = numeric_limits<int>::max();
}
for(int i = 0; i < N; i++) {
int largest = 0;
for(int j = 0; j < N; j++) {
if(matrix[i][j] > matrix[j][largest]) {
// records the position of the largest integer in each row
largest = j;
}
if(matrix[i][j] < smallestEachColumn[j]) {
smallestEachColumn[j] = matrix[i][j];
}
}
largestEachRow[i] = largest;
}
cout << "The qualified integers are at: \n";
for(int i = 0; i < N; i++) {
if(matrix[i][largestEachRow[i]] ==
smallestEachColumn[larestEachRow[i]]) {
cout << "row: " << i << " column: " << largestEachRow[i] << endl;
}
}
}
Time complexity O(N^2). Can't do better because integers are not sorted, to find out the largest integer in each row we must inspect every integer in each row.
- ljiatu@umich.edu December 26, 2012It should satisfy both requirements: largest in each row and smallest in that column.
- ljiatu@umich.edu December 26, 2012
I wonder if this is correct as it says in the problem that min is three not exactly and only three chars.
- ljiatu@umich.edu January 11, 2013