Skip to content

Commit 026123c

Browse files
committed
update: 添加问题“778.水位上升的泳池中游泳”的代码和题解 (#1154)
0778(word): AC.cpp+py+java+rust+go(#1153) cpp - AC,93.42%,92.11% py - AC,98.13%,59.33% java - AC,47.31%,97.31% go - AC,45.61%,89.47% rust - AC,38.46%,38.46% 本次更改中: 英语单词的背诵日期分别是:2025.10.3、10.4、10.5; 日语单词的背诵日期是2025.10.6。 修复《1290.二进制链表转整数:链表+进制转换》图地址 增加《1039.多边形三角剖分的最低得分:记忆化搜索(深度优先搜索)》封面图 增加《812.最大三角形面积:三角形面积公式考察(附三种公式)》封面图 Signed-off-by: LetMeFly666 <[email protected]>
1 parent d78184c commit 026123c

16 files changed

+647
-8
lines changed

.commitmsg

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
0976: AC.cpp+py+java+go+rust (#1149)
1+
0778(word): AC.cpp+py+java+rust+go(#1153)
22

3-
cpp - AC,58.29%,26.60%
4-
rust - AC,-%,100.00%
5-
java - AC,96.37%,99.05%
6-
go - AC,47.06%,100.00%
7-
py - AC,34.41%,25.76%
3+
cpp - AC,93.42%,92.11%
4+
py - AC,98.13%,59.33%
5+
java - AC,47.31%,97.31%
6+
go - AC,45.61%,89.47%
7+
rust - AC,38.46%,38.46%
8+
9+
本次更改中:
10+
英语单词的背诵日期分别是:2025.10.3、10.4、10.5;
11+
日语单词的背诵日期是2025.10.6。
12+
13+
修复《1290.二进制链表转整数:链表+进制转换》图地址
14+
增加《1039.多边形三角剖分的最低得分:记忆化搜索(深度优先搜索)》封面图
15+
增加《812.最大三角形面积:三角形面积公式考察(附三种公式)》封面图
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-06 12:17:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-06 12:31:27
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
static constexpr int direction[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
14+
public:
15+
int swimInWater(vector<vector<int>>& grid) {
16+
int n = grid.size(), m = grid[0].size();
17+
if (n == 1 && m == 1) {
18+
return grid[0][0]; // while的判断逻辑是入队时判断是否是终点
19+
}
20+
auto cmp = [&grid](pair<int, int>& a, pair<int, int>& b) {
21+
return grid[a.first][a.second] > grid[b.first][b.second]; // val小的优先
22+
};
23+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(cmp);
24+
vector<vector<bool>> visited(n, vector<bool>(m));
25+
pq.push({0, 0});
26+
visited[0][0] = true;
27+
while (true) {
28+
auto [x, y] = pq.top();
29+
pq.pop();
30+
for (int d = 0; d < 4; d++) {
31+
int nx = x + direction[d][0], ny = y + direction[d][1];
32+
if (nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny]) {
33+
continue;
34+
}
35+
grid[nx][ny] = max(grid[nx][ny], grid[x][y]);
36+
if (nx == n - 1 && ny == m - 1) {
37+
return grid[nx][ny];
38+
}
39+
visited[nx][ny] = true;
40+
pq.push({nx, ny});
41+
}
42+
}
43+
}
44+
};

Codes/0778-swim-in-rising-water.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-06 12:17:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-06 14:31:25
6+
*/
7+
package main
8+
9+
// The Wrong Version: should call heap.Pop(pq) but not pq.Pop()
10+
11+
import "container/heap"
12+
13+
type Item778 struct {
14+
x, y int
15+
val int
16+
}
17+
18+
type PriorityQueue778 []*Item778
19+
20+
var directions778 = [][]int{{0, 1}, {0, -1}, {-1, 0}, {1, 0}}
21+
22+
func swimInWater(grid [][]int) int {
23+
n, m := len(grid), len(grid[0])
24+
if n == 1 && m == 1 {
25+
return grid[0][0]
26+
}
27+
visited := make([][]bool, n)
28+
for i, _ := range visited {
29+
visited[i] = make([]bool, m)
30+
}
31+
32+
pq := &PriorityQueue778{}
33+
heap.Init(pq)
34+
pq.Push(&Item778{0, 0, grid[0][0]})
35+
visited[0][0] = true
36+
for true {
37+
item := pq.Pop().(*Item778)
38+
x, y, val := item.x, item.y, item.val
39+
for _, d := range directions778 {
40+
nx, ny := x + d[0], y + d[1]
41+
if nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny] {
42+
continue
43+
}
44+
grid[nx][ny] = max(grid[nx][ny], val)
45+
if nx == n - 1 && ny == m - 1 {
46+
return grid[nx][ny]
47+
}
48+
visited[nx][ny] = true
49+
pq.Push(&Item778{nx, ny, grid[nx][ny]})
50+
}
51+
}
52+
53+
return -1 // Fake Return
54+
}
55+
56+
func (pq PriorityQueue778) Len() int { return len(pq) }
57+
func (pq PriorityQueue778) Less(i, j int) bool { return pq[i].val < pq[j].val }
58+
func (pq PriorityQueue778) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
59+
func (pq *PriorityQueue778) Push(x any) { *pq = append(*pq, x.(*Item778)) }
60+
func (pq *PriorityQueue778) Pop() any { n := len(*pq); item := (*pq)[n - 1]; *pq = (*pq)[:n-1]; return item }
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-06 12:17:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-06 14:04:24
6+
*/
7+
import java.util.PriorityQueue;
8+
9+
class Solution {
10+
private static final int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
11+
12+
public int swimInWater(int[][] grid) {
13+
int n = grid.length, m = grid[0].length;
14+
if (n == 1 && m == 1) {
15+
return grid[0][0];
16+
}
17+
boolean[][] visited = new boolean[n][m];
18+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> grid[a[0]][a[1]] - grid[b[0]][b[1]]);
19+
pq.add(new int[]{0, 0});
20+
visited[0][0] = true;
21+
while (true) {
22+
int[] top = pq.poll();
23+
int x = top[0], y = top[1];
24+
for (int[] d : directions) {
25+
int nx = x + d[0], ny = y + d[1];
26+
if (nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny]) {
27+
continue;
28+
}
29+
grid[nx][ny] = Math.max(grid[nx][ny], grid[x][y]);
30+
if (nx == n - 1 && ny == m - 1) {
31+
return grid[nx][ny];
32+
}
33+
visited[nx][ny] = true;
34+
pq.add(new int[]{nx, ny});
35+
}
36+
}
37+
}
38+
}

Codes/0778-swim-in-rising-water.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-10-06 12:17:42
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-10-06 13:06:15
6+
'''
7+
from typing import List
8+
import heapq
9+
10+
class Solution:
11+
directions = [[0, 1], [0, -1], [-1, 0], [1, 0]]
12+
13+
def swimInWater(self, grid: List[List[int]]) -> int:
14+
n, m = len(grid), len(grid[0])
15+
if n == 1 and m == 1:
16+
return grid[0][0]
17+
visited = [[False] * m for _ in range(n)]
18+
pq = [(grid[0][0], 0, 0)]
19+
visited[0][0] = True
20+
while True:
21+
val, x, y = heapq.heappop(pq)
22+
for dx, dy in self.directions:
23+
nx, ny = x + dx, y + dy
24+
if (not (0 <= nx < n and 0 <= ny < m)) or visited[nx][ny]:
25+
continue
26+
grid[nx][ny] = max(grid[nx][ny], val)
27+
if nx == n - 1 and ny == m - 1:
28+
return grid[nx][ny]
29+
visited[nx][ny] = True
30+
heapq.heappush(pq, (grid[nx][ny], nx, ny))

Codes/0778-swim-in-rising-water.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-06 12:17:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-06 14:48:19
6+
*/
7+
use std::collections::BinaryHeap;
8+
use std::cmp::Reverse;
9+
10+
impl Solution {
11+
const DIRECTIONS: [[i32; 2]; 4] = [[0, 1], [0, -1], [-1, 0], [1, 0]];
12+
pub fn swim_in_water(grid: Vec<Vec<i32>>) -> i32 {
13+
let n: usize = grid.len();
14+
let m: usize = grid[0].len();
15+
if n == 1 && m == 1 {
16+
return grid[0][0];
17+
}
18+
let mut pq = BinaryHeap::new();
19+
let mut visited: Vec<Vec<bool>> = vec![vec![false; m]; n];
20+
pq.push(Reverse((grid[0][0], 0, 0)));
21+
visited[0][0] = true;
22+
23+
while let Some(Reverse((val, x, y))) = pq.pop() {
24+
for &[dx, dy] in Self::DIRECTIONS.iter() {
25+
let nx: usize = x + dx as usize;
26+
let ny: usize = y + dy as usize;
27+
if nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny] {
28+
continue;
29+
}
30+
let n_val: i32 = val.max(grid[nx][ny]);
31+
if nx == n - 1 && ny == m - 1 {
32+
return n_val;
33+
}
34+
visited[nx][ny] = true;
35+
pq.push(Reverse((n_val, nx, ny)));
36+
}
37+
}
38+
39+
-1 // Fake Return
40+
}
41+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-06 12:17:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-06 14:31:37
6+
*/
7+
package main
8+
9+
import "container/heap"
10+
11+
type Item778 struct {
12+
x, y int
13+
val int
14+
}
15+
16+
type PriorityQueue778 []*Item778
17+
18+
var directions778 = [][]int{{0, 1}, {0, -1}, {-1, 0}, {1, 0}}
19+
20+
func swimInWater(grid [][]int) int {
21+
n, m := len(grid), len(grid[0])
22+
if n == 1 && m == 1 {
23+
return grid[0][0]
24+
}
25+
visited := make([][]bool, n)
26+
for i, _ := range visited {
27+
visited[i] = make([]bool, m)
28+
}
29+
30+
pq := &PriorityQueue778{}
31+
heap.Init(pq)
32+
heap.Push(pq, &Item778{0, 0, grid[0][0]})
33+
visited[0][0] = true
34+
for true {
35+
item := heap.Pop(pq).(*Item778)
36+
x, y, val := item.x, item.y, item.val
37+
for _, d := range directions778 {
38+
nx, ny := x + d[0], y + d[1]
39+
if nx < 0 || nx >= n || ny < 0 || ny >= m || visited[nx][ny] {
40+
continue
41+
}
42+
grid[nx][ny] = max(grid[nx][ny], val)
43+
if nx == n - 1 && ny == m - 1 {
44+
return grid[nx][ny]
45+
}
46+
visited[nx][ny] = true
47+
heap.Push(pq, &Item778{nx, ny, grid[nx][ny]})
48+
}
49+
}
50+
51+
return -1 // Fake Return
52+
}
53+
54+
func (pq PriorityQueue778) Len() int { return len(pq) }
55+
func (pq PriorityQueue778) Less(i, j int) bool { return pq[i].val < pq[j].val }
56+
func (pq PriorityQueue778) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
57+
func (pq *PriorityQueue778) Push(x any) { *pq = append(*pq, x.(*Item778)) }
58+
func (pq *PriorityQueue778) Pop() any { n := len(*pq); item := (*pq)[n - 1]; *pq = (*pq)[:n-1]; return item }

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-10-01 20:40:27
66
*/
77
pub struct Solution;
8-
include!("0976-largest-perimeter-triangle.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("0778-swim-in-rising-water.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@
397397
|0769.最多能完成排序的块|中等|<a href="https://leetcode.cn/problems/max-chunks-to-make-sorted/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/13/LeetCode%200769.%E6%9C%80%E5%A4%9A%E8%83%BD%E5%AE%8C%E6%88%90%E6%8E%92%E5%BA%8F%E7%9A%84%E5%9D%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127295302" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/max-chunks-to-make-sorted/solution/letmefly-769zui-duo-neng-wan-cheng-pai-x-pzsz/" target="_blank">LeetCode题解</a>|
398398
|0771.宝石与石头|简单|<a href="https://leetcode.cn/problems/jewels-and-stones/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/24/LeetCode%200771.%E5%AE%9D%E7%9F%B3%E4%B8%8E%E7%9F%B3%E5%A4%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131888350" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/jewels-and-stones/solutions/2356279/letmefly-771bao-shi-yu-shi-tou-by-tisfy-fqmc/" target="_blank">LeetCode题解</a>|
399399
|0775.全局倒置与局部倒置|中等|<a href="https://leetcode.cn/problems/global-and-local-inversions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/16/LeetCode%200775.%E5%85%A8%E5%B1%80%E5%80%92%E7%BD%AE%E4%B8%8E%E5%B1%80%E9%83%A8%E5%80%92%E7%BD%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127877435" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/global-and-local-inversions/solutions/1973200/letmefly-775quan-ju-dao-zhi-yu-ju-bu-dao-zbqw/" target="_blank">LeetCode题解</a>|
400+
|0778.水位上升的泳池中游泳|困难|<a href="https://leetcode.cn/problems/swim-in-rising-water/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/10/06/LeetCode%200778.%E6%B0%B4%E4%BD%8D%E4%B8%8A%E5%8D%87%E7%9A%84%E6%B3%B3%E6%B1%A0%E4%B8%AD%E6%B8%B8%E6%B3%B3/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/152604318" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/swim-in-rising-water/solutions/3799229/778shui-wei-shang-sheng-de-yong-chi-zhon-j0la/" target="_blank">LeetCode题解</a>|
400401
|0779.第K个语法符号|中等|<a href="https://leetcode.cn/problems/k-th-symbol-in-grammar/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/20/LeetCode%200779.%E7%AC%ACK%E4%B8%AA%E8%AF%AD%E6%B3%95%E7%AC%A6%E5%8F%B7/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127419515" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/k-th-symbol-in-grammar/solution/letmefly-779di-kge-yu-fa-fu-hao-di-gui-z-9fs1/" target="_blank">LeetCode题解</a>|
401402
|0781.森林中的兔子|中等|<a href="https://leetcode.cn/problems/rabbits-in-forest/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/20/LeetCode%200781.%E6%A3%AE%E6%9E%97%E4%B8%AD%E7%9A%84%E5%85%94%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147375747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/rabbits-in-forest/solutions/3657091/letmefly-781sen-lin-zhong-de-tu-zi-si-we-bs2h/" target="_blank">LeetCode题解</a>|
402403
|0784.字母大小写全排列|中等|<a href="https://leetcode.cn/problems/letter-case-permutation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/30/LeetCode%200784.%E5%AD%97%E6%AF%8D%E5%A4%A7%E5%B0%8F%E5%86%99%E5%85%A8%E6%8E%92%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127595187" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/letter-case-permutation/solution/letmefly-784zi-mu-da-xiao-xie-quan-pai-l-5i9r/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)