|
40 | 40 |
|
41 | 41 | <!-- 这里可写通用的实现逻辑 -->
|
42 | 42 |
|
| 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 | + |
43 | 53 | <!-- tabs:start -->
|
44 | 54 |
|
45 | 55 | ### **Python3**
|
46 | 56 |
|
47 | 57 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
48 | 58 |
|
49 | 59 | ```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 |
51 | 73 | ```
|
52 | 74 |
|
53 | 75 | ### **Java**
|
54 | 76 |
|
55 | 77 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
56 | 78 |
|
57 | 79 | ```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 | +``` |
58 | 117 |
|
| 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 | +} |
59 | 142 | ```
|
60 | 143 |
|
61 | 144 | ### **...**
|
|
0 commit comments