Skip to content

Commit 7847ac0

Browse files
committed
add java solution for leetcode 778
1 parent d985da0 commit 7847ac0

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

solution/0700-0799/0778.Swim in Rising Water/README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,56 @@
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```java
67-
67+
class Solution {
68+
// x、y方向向量
69+
public static final int[] dx = {0, 0, 1, -1};
70+
public static final int[] dy = {1, -1, 0, 0};
71+
/**
72+
* https://blog.csdn.net/fuxuemingzhu/article/details/82926674
73+
* <p>
74+
* 参考这篇文章的第二种解题方法做的
75+
* <p>
76+
* 通过优先级队列找寻局部最优解 最终的得到的结果就是全局最优解
77+
*
78+
* @param grid
79+
* @return
80+
*/
81+
// 以grid左上角为原点,横向为X轴,纵向为Y轴
82+
public int swimInWater(int[][] grid) {
83+
// 定义一个优先级队列 按照h从小到大排列
84+
Queue<Pair<Integer, Pair<Integer, Integer>>> queue = new PriorityQueue<>(Comparator.comparing(Pair::getKey));
85+
queue.add(new Pair<>(grid[0][0], new Pair<>(0, 0)));
86+
// 已经遍历过的点
87+
Set<Pair<Integer, Integer>> visitSet = new HashSet<>();
88+
visitSet.add(new Pair<>(0, 0));
89+
90+
int res = 0;
91+
int length = grid.length;
92+
93+
while (!queue.isEmpty()) {
94+
Pair<Integer, Pair<Integer, Integer>> top = queue.poll();
95+
Integer x = top.getValue().getKey();
96+
Integer y = top.getValue().getValue();
97+
res = Math.max(res, top.getKey());
98+
// 2 <= N <= 50 这个范围内可以直接使用==进行Integer的比较
99+
if (x == top.getValue().getValue() && y == length - 1) {
100+
break;
101+
}
102+
103+
for (int i = 0; i < 4; i++) {
104+
int newY = y + dy[i];
105+
int newX = x + dx[i];
106+
if (newX < 0 || newY < 0 || newX >= length || newY >= length || visitSet.contains(new Pair<>(newX, newY))) {
107+
// 直接忽略
108+
continue;
109+
}
110+
queue.add(new Pair<>(grid[newX][newY], new Pair<>(newX, newY)));
111+
visitSet.add(new Pair<>(newX, newY));
112+
}
113+
}
114+
return res;
115+
}
116+
}
68117
```
69118

70119
### **...**

solution/0700-0799/0778.Swim in Rising Water/README_EN.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,56 @@ We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
102102
### **Java**
103103

104104
```java
105-
105+
class Solution {
106+
// x、y方向向量
107+
public static final int[] dx = {0, 0, 1, -1};
108+
public static final int[] dy = {1, -1, 0, 0};
109+
/**
110+
* https://blog.csdn.net/fuxuemingzhu/article/details/82926674
111+
* <p>
112+
* 参考这篇文章的第二种解题方法做的
113+
* <p>
114+
* 通过优先级队列找寻局部最优解 最终的得到的结果就是全局最优解
115+
*
116+
* @param grid
117+
* @return
118+
*/
119+
// 以grid左上角为原点,横向为X轴,纵向为Y轴
120+
public int swimInWater(int[][] grid) {
121+
// 定义一个优先级队列 按照h从小到大排列
122+
Queue<Pair<Integer, Pair<Integer, Integer>>> queue = new PriorityQueue<>(Comparator.comparing(Pair::getKey));
123+
queue.add(new Pair<>(grid[0][0], new Pair<>(0, 0)));
124+
// 已经遍历过的点
125+
Set<Pair<Integer, Integer>> visitSet = new HashSet<>();
126+
visitSet.add(new Pair<>(0, 0));
127+
128+
int res = 0;
129+
int length = grid.length;
130+
131+
while (!queue.isEmpty()) {
132+
Pair<Integer, Pair<Integer, Integer>> top = queue.poll();
133+
Integer x = top.getValue().getKey();
134+
Integer y = top.getValue().getValue();
135+
res = Math.max(res, top.getKey());
136+
// 2 <= N <= 50 这个范围内可以直接使用==进行Integer的比较
137+
if (x == top.getValue().getValue() && y == length - 1) {
138+
break;
139+
}
140+
141+
for (int i = 0; i < 4; i++) {
142+
int newY = y + dy[i];
143+
int newX = x + dx[i];
144+
if (newX < 0 || newY < 0 || newX >= length || newY >= length || visitSet.contains(new Pair<>(newX, newY))) {
145+
// 直接忽略
146+
continue;
147+
}
148+
queue.add(new Pair<>(grid[newX][newY], new Pair<>(newX, newY)));
149+
visitSet.add(new Pair<>(newX, newY));
150+
}
151+
}
152+
return res;
153+
}
154+
}
106155
```
107156

108157
### **...**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy