Skip to content

add java solution for leetcode 778 #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion solution/0700-0799/0778.Swim in Rising Water/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,56 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
// x、y方向向量
public static final int[] dx = {0, 0, 1, -1};
public static final int[] dy = {1, -1, 0, 0};
/**
* https://blog.csdn.net/fuxuemingzhu/article/details/82926674
* <p>
* 参考这篇文章的第二种解题方法做的
* <p>
* 通过优先级队列找寻局部最优解 最终的得到的结果就是全局最优解
*
* @param grid
* @return
*/
// 以grid左上角为原点,横向为X轴,纵向为Y轴
public int swimInWater(int[][] grid) {
// 定义一个优先级队列 按照h从小到大排列
Queue<Pair<Integer, Pair<Integer, Integer>>> queue = new PriorityQueue<>(Comparator.comparing(Pair::getKey));
queue.add(new Pair<>(grid[0][0], new Pair<>(0, 0)));
// 已经遍历过的点
Set<Pair<Integer, Integer>> visitSet = new HashSet<>();
visitSet.add(new Pair<>(0, 0));

int res = 0;
int length = grid.length;

while (!queue.isEmpty()) {
Pair<Integer, Pair<Integer, Integer>> top = queue.poll();
Integer x = top.getValue().getKey();
Integer y = top.getValue().getValue();
res = Math.max(res, top.getKey());
// 2 <= N <= 50 这个范围内可以直接使用==进行Integer的比较
if (x == top.getValue().getValue() && y == length - 1) {
break;
}

for (int i = 0; i < 4; i++) {
int newY = y + dy[i];
int newX = x + dx[i];
if (newX < 0 || newY < 0 || newX >= length || newY >= length || visitSet.contains(new Pair<>(newX, newY))) {
// 直接忽略
continue;
}
queue.add(new Pair<>(grid[newX][newY], new Pair<>(newX, newY)));
visitSet.add(new Pair<>(newX, newY));
}
}
return res;
}
}
```

### **...**
Expand Down
51 changes: 50 additions & 1 deletion solution/0700-0799/0778.Swim in Rising Water/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,56 @@ We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
### **Java**

```java

class Solution {
// x、y方向向量
public static final int[] dx = {0, 0, 1, -1};
public static final int[] dy = {1, -1, 0, 0};
/**
* https://blog.csdn.net/fuxuemingzhu/article/details/82926674
* <p>
* 参考这篇文章的第二种解题方法做的
* <p>
* 通过优先级队列找寻局部最优解 最终的得到的结果就是全局最优解
*
* @param grid
* @return
*/
// 以grid左上角为原点,横向为X轴,纵向为Y轴
public int swimInWater(int[][] grid) {
// 定义一个优先级队列 按照h从小到大排列
Queue<Pair<Integer, Pair<Integer, Integer>>> queue = new PriorityQueue<>(Comparator.comparing(Pair::getKey));
queue.add(new Pair<>(grid[0][0], new Pair<>(0, 0)));
// 已经遍历过的点
Set<Pair<Integer, Integer>> visitSet = new HashSet<>();
visitSet.add(new Pair<>(0, 0));

int res = 0;
int length = grid.length;

while (!queue.isEmpty()) {
Pair<Integer, Pair<Integer, Integer>> top = queue.poll();
Integer x = top.getValue().getKey();
Integer y = top.getValue().getValue();
res = Math.max(res, top.getKey());
// 2 <= N <= 50 这个范围内可以直接使用==进行Integer的比较
if (x == top.getValue().getValue() && y == length - 1) {
break;
}

for (int i = 0; i < 4; i++) {
int newY = y + dy[i];
int newX = x + dx[i];
if (newX < 0 || newY < 0 || newX >= length || newY >= length || visitSet.contains(new Pair<>(newX, newY))) {
// 直接忽略
continue;
}
queue.add(new Pair<>(grid[newX][newY], new Pair<>(newX, newY)));
visitSet.add(new Pair<>(newX, newY));
}
}
return res;
}
}
```

### **...**
Expand Down