Skip to content

Conversation

@altay9
Copy link
Collaborator

@altay9 altay9 commented Apr 28, 2021

It is possible to solve this question using PriorityQueue, Union Find, Binary Search.

Binary Search looks like the fastest one and it is based on the idea that the value should be in a range between 0 and (high = Math.min(A[0][0], A[row-1][col-1])) as it is certain that any possible path must include both of these nodes.

Refer to:#50 for a detailed explanation (currently in Turkish)

image

@altay9 altay9 changed the title Refer to:https://github.com/spiralgo/algorithms/issues/50 1102. Path With Maximum Minimum Value Apr 28, 2021
@ErdemT09
Copy link
Collaborator

ErdemT09 commented Apr 30, 2021

n ve m grid boyutları diyelim. A ise 0,0 noktasının ve bitiş noktasının değerlerinin en azı olsun (high değişkeninin bağlı olduğu değer).
Bunun zaman karmaşıklığı o zaman O(m*n*log(A)) mı oluyor? Her bölerek aramada tüm gridi dolanıyoruz. Bunu log(A)'ya bağlı bir biçimde yapacağımız böyle düşündüm.

@altay9
Copy link
Collaborator Author

altay9 commented Apr 30, 2021

Evet, haklısın.
Soruda şöyle verilmiş:
0 <= A[i][j] <= 10^9

Demek ki, Binary Search en azından şöyle arayacak:
O(log n) = O(log 10^9).
2 tabanında log 10^9= 29.897352854 ediyor. Yani, yaklaşık 30 adımda, halletmiş olacak.
isThereAPossiblePath için de muhtemelen n*m gibi bir yük eklenir.
Dolayısı ile, doğru söylüyorsun.

@ErdemT09
Copy link
Collaborator

I think we can move some variables out into the class field, so that the code is cleaner with the isThereAPossiblePath method:

final static int DIRECTIONS[] = {0, 1, 0, -1, 0};

    private int rowCount, colCount;
    private boolean[][] visited;
    private int[][] grid;
    public int maximumMinimumPath(int[][] A) {
        grid = A;
        rowCount = A.length;
        colCount = A[0].length;

        int low = 1, high = Math.min(A[0][0], A[rowCount - 1][colCount - 1]) + 1;
      
        while (low < high) {
            int mid = (low + high) / 2;
            visited = new boolean[rowCount][colCount];
            if (isThereAPossiblePath( 0, 0, mid)) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }

        return low - 1;
    }

    boolean isThereAPossiblePath(int x, int y, int limit) {
        if (x == rowCount - 1 && y == colCount - 1) {
            return true;
        }
        visited[x][y] = true;

        for (int i = 0; i < 4; i++) {
            int dx = x + DIRECTIONS[i];
            int dy = y + DIRECTIONS[i + 1];
            if (dx >= 0 && dy >= 0 && dx <= rowCount - 1 && dy <= colCount - 1 && grid[dx][dy] >= limit && !visited[dx][dy]) {
                if (isThereAPossiblePath(dx, dy, limit)) {
                    return true;
                }
            }

        }
        return false;

    }

@altay9
Copy link
Collaborator Author

altay9 commented Apr 30, 2021

You are absolutely right. I refactored and pushed again.

@ErdemT09
Copy link
Collaborator

ErdemT09 commented Apr 30, 2021

To consider the patterns from this answer, I would say that the method isThereAPossiblePath utilizes a kind of DFS. This kind of grid traversal with the boolean matrix keeping visited nodes is also notable. Noticing that the wanted return value is something 0 and the minimum value of the both ends of the grid is the another key point in this answer.
Besides these, showing directions like final static int DIRECTIONS[] = {0, 1, 0, -1, 0}; was unreadable at first. But then I have noticed how it works and this is shorter than writing all the 4 directions. Some small, but smart detail here.
So, I give an approval to this commit. However, I think that we should eventually at some point add the queue and the union set solutions to this question as well.

@altay9 altay9 merged commit 51f06e1 into master Apr 30, 2021
@altay9 altay9 deleted the PathWithMaximumMinimumValue branch April 30, 2021 18:56
@ErdemT09 ErdemT09 mentioned this pull request Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants