Skip to content

Commit 8e1d795

Browse files
committed
feat: add solutions to lc problems: No.0417,0739
- No.0417.Pacific Atlantic Water Flow - No.0739.Daily Temperatures
1 parent 9f8087b commit 8e1d795

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

solution/0400-0499/0417.Pacific Atlantic Water Flow/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,61 @@ func pacificAtlantic(heights [][]int) [][]int {
281281
}
282282
```
283283

284+
### **TypeScript**
285+
286+
```ts
287+
function pacificAtlantic(heights: number[][]): number[][] {
288+
const m = heights.length;
289+
const n = heights[0].length;
290+
const dirs = [
291+
[1, 0],
292+
[0, 1],
293+
[-1, 0],
294+
[0, -1],
295+
];
296+
const gird = new Array(m).fill(0).map(() => new Array(n).fill(0));
297+
const isVis = new Array(m).fill(0).map(() => new Array(n).fill(false));
298+
299+
const dfs = (i: number, j: number) => {
300+
if (isVis[i][j]) {
301+
return;
302+
}
303+
gird[i][j]++;
304+
isVis[i][j] = true;
305+
const h = heights[i][j];
306+
for (const [x, y] of dirs) {
307+
if (h <= (heights[i + x] ?? [])[j + y]) {
308+
dfs(i + x, j + y);
309+
}
310+
}
311+
};
312+
313+
for (let i = 0; i < n; i++) {
314+
dfs(0, i);
315+
}
316+
for (let i = 0; i < m; i++) {
317+
dfs(i, 0);
318+
}
319+
isVis.forEach(v => v.fill(false));
320+
for (let i = 0; i < n; i++) {
321+
dfs(m - 1, i);
322+
}
323+
for (let i = 0; i < m; i++) {
324+
dfs(i, n - 1);
325+
}
326+
327+
const res = [];
328+
for (let i = 0; i < m; i++) {
329+
for (let j = 0; j < n; j++) {
330+
if (gird[i][j] === 2) {
331+
res.push([i, j]);
332+
}
333+
}
334+
}
335+
return res;
336+
}
337+
```
338+
284339
### **...**
285340

286341
```

solution/0400-0499/0417.Pacific Atlantic Water Flow/README_EN.md

+55
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,61 @@ func pacificAtlantic(heights [][]int) [][]int {
267267
}
268268
```
269269

270+
### **TypeScript**
271+
272+
```ts
273+
function pacificAtlantic(heights: number[][]): number[][] {
274+
const m = heights.length;
275+
const n = heights[0].length;
276+
const dirs = [
277+
[1, 0],
278+
[0, 1],
279+
[-1, 0],
280+
[0, -1],
281+
];
282+
const gird = new Array(m).fill(0).map(() => new Array(n).fill(0));
283+
const isVis = new Array(m).fill(0).map(() => new Array(n).fill(false));
284+
285+
const dfs = (i: number, j: number) => {
286+
if (isVis[i][j]) {
287+
return;
288+
}
289+
gird[i][j]++;
290+
isVis[i][j] = true;
291+
const h = heights[i][j];
292+
for (const [x, y] of dirs) {
293+
if (h <= (heights[i + x] ?? [])[j + y]) {
294+
dfs(i + x, j + y);
295+
}
296+
}
297+
};
298+
299+
for (let i = 0; i < n; i++) {
300+
dfs(0, i);
301+
}
302+
for (let i = 0; i < m; i++) {
303+
dfs(i, 0);
304+
}
305+
isVis.forEach(v => v.fill(false));
306+
for (let i = 0; i < n; i++) {
307+
dfs(m - 1, i);
308+
}
309+
for (let i = 0; i < m; i++) {
310+
dfs(i, n - 1);
311+
}
312+
313+
const res = [];
314+
for (let i = 0; i < m; i++) {
315+
for (let j = 0; j < n; j++) {
316+
if (gird[i][j] === 2) {
317+
res.push([i, j]);
318+
}
319+
}
320+
}
321+
return res;
322+
}
323+
```
324+
270325
### **...**
271326

272327
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function pacificAtlantic(heights: number[][]): number[][] {
2+
const m = heights.length;
3+
const n = heights[0].length;
4+
const dirs = [
5+
[1, 0],
6+
[0, 1],
7+
[-1, 0],
8+
[0, -1],
9+
];
10+
const gird = new Array(m).fill(0).map(() => new Array(n).fill(0));
11+
const isVis = new Array(m).fill(0).map(() => new Array(n).fill(false));
12+
13+
const dfs = (i: number, j: number) => {
14+
if (isVis[i][j]) {
15+
return;
16+
}
17+
gird[i][j]++;
18+
isVis[i][j] = true;
19+
const h = heights[i][j];
20+
for (const [x, y] of dirs) {
21+
if (h <= (heights[i + x] ?? [])[j + y]) {
22+
dfs(i + x, j + y);
23+
}
24+
}
25+
};
26+
27+
for (let i = 0; i < n; i++) {
28+
dfs(0, i);
29+
}
30+
for (let i = 0; i < m; i++) {
31+
dfs(i, 0);
32+
}
33+
isVis.forEach(v => v.fill(false));
34+
for (let i = 0; i < n; i++) {
35+
dfs(m - 1, i);
36+
}
37+
for (let i = 0; i < m; i++) {
38+
dfs(i, n - 1);
39+
}
40+
41+
const res = [];
42+
for (let i = 0; i < m; i++) {
43+
for (let j = 0; j < n; j++) {
44+
if (gird[i][j] === 2) {
45+
res.push([i, j]);
46+
}
47+
}
48+
}
49+
return res;
50+
}

solution/0700-0799/0739.Daily Temperatures/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ func dailyTemperatures(temperatures []int) []int {
137137
}
138138
```
139139

140+
### **Rust**
141+
142+
```rust
143+
impl Solution {
144+
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
145+
let n = temperatures.len();
146+
let mut stack = vec![];
147+
let mut res = vec![0; n];
148+
for i in 0..n {
149+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] < temperatures[i] {
150+
let j = stack.pop().unwrap();
151+
res[j] = (i - j) as i32;
152+
}
153+
stack.push(i);
154+
}
155+
res
156+
}
157+
}
158+
```
159+
140160
### **...**
141161

142162
```

solution/0700-0799/0739.Daily Temperatures/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ func dailyTemperatures(temperatures []int) []int {
113113
}
114114
```
115115

116+
### **Rust**
117+
118+
```rust
119+
impl Solution {
120+
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
121+
let n = temperatures.len();
122+
let mut stack = vec![];
123+
let mut res = vec![0; n];
124+
for i in 0..n {
125+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] < temperatures[i] {
126+
let j = stack.pop().unwrap();
127+
res[j] = (i - j) as i32;
128+
}
129+
stack.push(i);
130+
}
131+
res
132+
}
133+
}
134+
```
135+
116136
### **...**
117137

118138
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn daily_temperatures(temperatures: Vec<i32>) -> Vec<i32> {
3+
let n = temperatures.len();
4+
let mut stack = vec![];
5+
let mut res = vec![0; n];
6+
for i in 0..n {
7+
while !stack.is_empty() && temperatures[*stack.last().unwrap()] < temperatures[i] {
8+
let j = stack.pop().unwrap();
9+
res[j] = (i - j) as i32;
10+
}
11+
stack.push(i);
12+
}
13+
res
14+
}
15+
}

0 commit comments

Comments
 (0)