Skip to content

Commit 2644bd7

Browse files
authored
feat: add solutions to lcp problem: No.61 (#3138)
No.61.气温变化趋势
1 parent 3567cdc commit 2644bd7

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

lcp/LCP 61. 气温变化趋势/README.md

+49-6
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94%
5050

5151
<!-- solution:start -->
5252

53-
### 方法一:动态规划
53+
### 方法一:一次遍历
5454

5555
我们用变量 $f$ 维护当前趋势相同的连续天数,用变量 $ans$ 维护最大的连续天数。
5656

5757
遍历数组,对于第 $i$ 天,记两地的气温变化趋势分别为 $x$ 和 $y$,如果 $x$ 和 $y$ 均为 $0$ 或者 $x$ 和 $y$ 均为正数或负数,则说明第 $i$ 天和第 $i+1$ 天的气温变化趋势相同,此时 $f$ 自增 $1$,并更新 $ans$;否则说明第 $i$ 天和第 $i+1$ 天的气温变化趋势不同,此时 $f$ 重置为 $0$。
5858

5959
最终返回 $ans$ 即可。
6060

61-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度
61+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
6262

6363
<!-- tabs:start -->
6464

@@ -68,10 +68,8 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94%
6868
class Solution:
6969
def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
7070
ans = f = 0
71-
n = len(temperatureA)
72-
for i in range(n - 1):
73-
x = temperatureA[i + 1] - temperatureA[i]
74-
y = temperatureB[i + 1] - temperatureB[i]
71+
for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):
72+
x, y = a2 - a1, b2 - b1
7573
if x == y == 0 or x * y > 0:
7674
f += 1
7775
ans = max(ans, f)
@@ -140,6 +138,51 @@ func temperatureTrend(temperatureA []int, temperatureB []int) int {
140138
}
141139
```
142140

141+
#### TypeScript
142+
143+
```ts
144+
function temperatureTrend(temperatureA: number[], temperatureB: number[]): number {
145+
let [ans, f] = [0, 0];
146+
for (let i = 0; i < temperatureA.length - 1; ++i) {
147+
let x = temperatureA[i + 1] - temperatureA[i];
148+
let y = temperatureB[i + 1] - temperatureB[i];
149+
if ((x === 0 && y === 0) || x * y > 0) {
150+
ans = Math.max(ans, ++f);
151+
} else {
152+
f = 0;
153+
}
154+
}
155+
return ans;
156+
}
157+
```
158+
159+
#### Rust
160+
161+
```rust
162+
impl Solution {
163+
pub fn temperature_trend(temperature_a: Vec<i32>, temperature_b: Vec<i32>) -> i32 {
164+
let mut ans = 0;
165+
let mut f = 0;
166+
167+
for i in 0..temperature_a.len() - 1 {
168+
let x = temperature_a[i + 1] - temperature_a[i];
169+
let y = temperature_b[i + 1] - temperature_b[i];
170+
171+
if (x == 0 && y == 0) || (x > 0 && y > 0) || (x < 0 && y < 0) {
172+
f += 1;
173+
if f > ans {
174+
ans = f;
175+
}
176+
} else {
177+
f = 0;
178+
}
179+
}
180+
181+
ans
182+
}
183+
}
184+
```
185+
143186
<!-- tabs:end -->
144187

145188
<!-- solution:end -->

lcp/LCP 61. 气温变化趋势/Solution.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution:
22
def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
33
ans = f = 0
4-
n = len(temperatureA)
5-
for i in range(n - 1):
6-
x = temperatureA[i + 1] - temperatureA[i]
7-
y = temperatureB[i + 1] - temperatureB[i]
4+
for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)):
5+
x, y = a2 - a1, b2 - b1
86
if x == y == 0 or x * y > 0:
97
f += 1
108
ans = max(ans, f)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn temperature_trend(temperature_a: Vec<i32>, temperature_b: Vec<i32>) -> i32 {
3+
let mut ans = 0;
4+
let mut f = 0;
5+
6+
for i in 0..temperature_a.len() - 1 {
7+
let x = temperature_a[i + 1] - temperature_a[i];
8+
let y = temperature_b[i + 1] - temperature_b[i];
9+
10+
if (x == 0 && y == 0) || (x > 0 && y > 0) || (x < 0 && y < 0) {
11+
f += 1;
12+
if f > ans {
13+
ans = f;
14+
}
15+
} else {
16+
f = 0;
17+
}
18+
}
19+
20+
ans
21+
}
22+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function temperatureTrend(temperatureA: number[], temperatureB: number[]): number {
2+
let [ans, f] = [0, 0];
3+
for (let i = 0; i < temperatureA.length - 1; ++i) {
4+
let x = temperatureA[i + 1] - temperatureA[i];
5+
let y = temperatureB[i + 1] - temperatureB[i];
6+
if ((x === 0 && y === 0) || x * y > 0) {
7+
ans = Math.max(ans, ++f);
8+
} else {
9+
f = 0;
10+
}
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)