D
D
Dred Wolf2020-11-03 17:34:22
C++ / C#
Dred Wolf, 2020-11-03 17:34:22

How to find the maximum among all local minima of a given matrix?

What are some ideas for solving this problem.
I know that you need to first find the minimums, and among them to find the maximum.
The minimum (as well as the maximum) can be found by comparing an element with its left, right, top, and bottom elements.
There are 3 options for the location of the matrix element:
1) Corner (has two neighbors)
2) Frame (has three neighbors, these are elements from the edges of the matrix)
3) Internal (have 4 neighbors)

How can this problem be solved using the C language?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ananiev, 2020-11-03
@SaNNy32

The idea is this: make a solution algorithm on a piece of paper, then program it.
Do a manual search on different matrices, as you find a pattern, you can write an algorithm.

W
Wataru, 2020-11-04
@wataru

The algorithm is trivial - iterate over all the cells of the matrix in two nested loops. Check that the current cell is a local minimum (compare with all neighbors). If so, then you need to compare it with the current candidate to the maximum.
In fact, there are 2 nested tasks here: 1) Check if the value is a local minimum 2) Find the maximum in the matrix (possibly ignoring some cells).
I advise you to solve problems separately using functions.
For the first task, write a function.

bool IsLocalMinumum(int a[][m], int n, int m, int i, int j);

The function must go through 4 options (add/subtract 1 from the first or second index) and for each check: 1) there is a neighbor, i.e. did not go beyond the border, 2) the value of the neighbor is less than the current one. If both conditions are met for some direction - you have found a "bad" neighbor. The current cell cannot be the minimum. Return false immediately. Always return true at the end of the function.
The second task is quite trivial. Can you pick the maximum from just a one-dimensional array? Now, instead of one loop over the array, do 2 nested loops over the matrix. Then add a condition there that calls your IsLocalMinimum. If the top is not a minimum - just skip the cell. Literallyif (!IsLocalMinimum(a, n, m, i, j)) continue;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question