Skip to content

Commit 655ede2

Browse files
committed
feat: add solutions to lcp problem: No.61
1 parent ef23e14 commit 655ede2

File tree

5 files changed

+149
-1
lines changed

5 files changed

+149
-1
lines changed

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

+84-1
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,105 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:动态规划**
44+
45+
我们用变量 $f$ 维护当前趋势相同的连续天数,用变量 $ans$ 维护最大的连续天数。
46+
47+
遍历数组,对于第 $i$ 天,记两地的气温变化趋势分别为 $x$ 和 $y$,如果 $x$ 和 $y$ 均为 $0$ 或者 $x$ 和 $y$ 均为正数或负数,则说明第 $i$ 天和第 $i+1$ 天的气温变化趋势相同,此时 $f$ 自增 $1$,并更新 $ans$;否则说明第 $i$ 天和第 $i+1$ 天的气温变化趋势不同,此时 $f$ 重置为 $0$。
48+
49+
最终返回 $ans$ 即可。
50+
51+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
52+
4353
<!-- tabs:start -->
4454

4555
### **Python3**
4656

4757
<!-- 这里可写当前语言的特殊实现逻辑 -->
4858

4959
```python
50-
60+
class Solution:
61+
def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
62+
ans = f = 0
63+
n = len(temperatureA)
64+
for i in range(n - 1):
65+
x = temperatureA[i + 1] - temperatureA[i]
66+
y = temperatureB[i + 1] - temperatureB[i]
67+
if x == y == 0 or x * y > 0:
68+
f += 1
69+
ans = max(ans, f)
70+
else:
71+
f = 0
72+
return ans
5173
```
5274

5375
### **Java**
5476

5577
<!-- 这里可写当前语言的特殊实现逻辑 -->
5678

5779
```java
80+
class Solution {
81+
public int temperatureTrend(int[] temperatureA, int[] temperatureB) {
82+
int ans = 0, f = 0;
83+
for (int i = 0; i < temperatureA.length - 1; ++i) {
84+
int x = temperatureA[i + 1] - temperatureA[i];
85+
int y = temperatureB[i + 1] - temperatureB[i];
86+
if ((x == 0 && y == 0) || x * y > 0) {
87+
ans = Math.max(ans, ++f);
88+
} else {
89+
f = 0;
90+
}
91+
}
92+
return ans;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int temperatureTrend(vector<int>& temperatureA, vector<int>& temperatureB) {
103+
int ans = 0, f = 0;
104+
for (int i = 0; i < temperatureA.size() - 1; ++i) {
105+
int x = temperatureA[i + 1] - temperatureA[i];
106+
int y = temperatureB[i + 1] - temperatureB[i];
107+
if ((x == 0 && y == 0) || x * y > 0) {
108+
ans = max(ans, ++f);
109+
} else {
110+
f = 0;
111+
}
112+
}
113+
return ans;
114+
}
115+
};
116+
```
58117
118+
### **Go**
119+
120+
```go
121+
func temperatureTrend(temperatureA []int, temperatureB []int) int {
122+
ans, f := 0, 0
123+
for i := range temperatureA[1:] {
124+
x := temperatureA[i+1] - temperatureA[i]
125+
y := temperatureB[i+1] - temperatureB[i]
126+
if (x == 0 && y == 0) || x*y > 0 {
127+
f++
128+
ans = max(ans, f)
129+
} else {
130+
f = 0
131+
}
132+
}
133+
return ans
134+
}
135+
136+
func max(a, b int) int {
137+
if a > b {
138+
return a
139+
}
140+
return b
141+
}
59142
```
60143

61144
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int temperatureTrend(vector<int>& temperatureA, vector<int>& temperatureB) {
4+
int ans = 0, f = 0;
5+
for (int i = 0; i < temperatureA.size() - 1; ++i) {
6+
int x = temperatureA[i + 1] - temperatureA[i];
7+
int y = temperatureB[i + 1] - temperatureB[i];
8+
if ((x == 0 && y == 0) || x * y > 0) {
9+
ans = max(ans, ++f);
10+
} else {
11+
f = 0;
12+
}
13+
}
14+
return ans;
15+
}
16+
};
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func temperatureTrend(temperatureA []int, temperatureB []int) int {
2+
ans, f := 0, 0
3+
for i := range temperatureA[1:] {
4+
x := temperatureA[i+1] - temperatureA[i]
5+
y := temperatureB[i+1] - temperatureB[i]
6+
if (x == 0 && y == 0) || x*y > 0 {
7+
f++
8+
ans = max(ans, f)
9+
} else {
10+
f = 0
11+
}
12+
}
13+
return ans
14+
}
15+
16+
func max(a, b int) int {
17+
if a > b {
18+
return a
19+
}
20+
return b
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int temperatureTrend(int[] temperatureA, int[] temperatureB) {
3+
int ans = 0, f = 0;
4+
for (int i = 0; i < temperatureA.length - 1; ++i) {
5+
int x = temperatureA[i + 1] - temperatureA[i];
6+
int y = temperatureB[i + 1] - temperatureB[i];
7+
if ((x == 0 && y == 0) || x * y > 0) {
8+
ans = Math.max(ans, ++f);
9+
} else {
10+
f = 0;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
3+
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]
8+
if x == y == 0 or x * y > 0:
9+
f += 1
10+
ans = max(ans, f)
11+
else:
12+
f = 0
13+
return ans

0 commit comments

Comments
 (0)