LCP 61. 气温变化趋势
力扣城计划在两地设立「力扣嘉年华」的分会场,气象小组正在分析两地区的气温变化趋势,对于第 i ~ (i+1)
天的气温变化趋势,将根据以下规则判断:
i+1
天的气温 高于 第 i
天,为 上升 趋势i+1
天的气温 等于 第 i
天,为 平稳 趋势i+1
天的气温 低于 第 i
天,为 下降 趋势已知 temperatureA[i]
和 temperatureB[i]
分别表示第 i
天两地区的气温。
组委会希望找到一段天数尽可能多,且两地气温变化趋势相同的时间举办嘉年华活动。请分析并返回两地气温变化趋势相同的最大连续天数。
即最大的
n
,使得第i~i+n
天之间,两地气温变化趋势相同
示例 1:
输入:
temperatureA = [21,18,18,18,31]
temperatureB = [34,32,16,16,17]
输出:
2
解释:如下表所示, 第
2~4
天两地气温变化趋势相同,且持续时间最长,因此返回4-2=2
{:width=1000px}
示例 2:
输入:
temperatureA = [5,10,16,-6,15,11,3]
temperatureB = [16,22,23,23,25,3,-16]
输出:
3
提示:
2 <= temperatureA.length == temperatureB.length <= 1000
-20 <= temperatureA[i], temperatureB[i] <= 40
原站题解
javascript 解法, 执行用时: 62 ms, 内存消耗: 49.7 MB, 提交时间: 2024-06-21 07:23:38
/** * @param {number[]} temperatureA * @param {number[]} temperatureB * @return {number} */ var temperatureTrend = function(a, b) { let ans = 0, same = 0; for (let i = 1; i < a.length; i++) { if (Math.sign(a[i - 1] - a[i]) == Math.sign(b[i - 1] - b[i])) { ans = Math.max(ans, ++same); } else { same = 0; } } return ans; };
rust 解法, 执行用时: 0 ms, 内存消耗: 2.2 MB, 提交时间: 2023-09-13 16:04:53
impl Solution { // 双指针 pub fn temperature_trend(temperature_a: Vec<i32>, temperature_b: Vec<i32>) -> i32 { let (a, b) = (temperature_a, temperature_b); let is_same_temp = |i: usize| a[i - 1].cmp(&a[i]) == b[i - 1].cmp(&b[i]); let len = a.len(); let (mut ans, mut i) = (0, 0); while i < len - 1 { let mut j = i + 1; while j < len && is_same_temp(j) { j += 1; } ans = ans.max(j - i - 1); i = j; } ans as i32 } // 函数式 pub fn temperature_trend2(temperature_a: Vec<i32>, temperature_b: Vec<i32>) -> i32 { (0..temperature_a.len()-1).fold((0, 0), |(ans, cnt), i| { if temperature_a[i+1].cmp(&temperature_a[i]) == temperature_b[i+1].cmp(&temperature_b[i]) { (ans.max(cnt + 1), cnt + 1) } else { (ans, 0) } }).0 } }
cpp 解法, 执行用时: 16 ms, 内存消耗: 12.4 MB, 提交时间: 2023-09-13 16:00:36
class Solution { public: int temperatureTrend(vector<int>& a, vector<int>& b) { int start = 0, ans = 0; for ( int i = 1; i < a.size(); ++i ) { if ( a[i] == a[i-1] != (b[i] == b[i-1]) || a[i] < a[i-1] != (b[i] < b[i-1]) ) { ans = max(ans, i-start-1); start = i; } } return max(ans, (int) (a.size()) - start - 1); } };
java 解法, 执行用时: 1 ms, 内存消耗: 42 MB, 提交时间: 2023-09-13 15:55:55
class Solution { public int temperatureTrend(int[] a, int[] b) { int start = 0, ans = 0; for ( int i = 1; i < a.length; ++i ) { if ( a[i] == a[i-1] != (b[i] == b[i-1]) || a[i] < a[i-1] != (b[i] < b[i-1]) ) { ans = Math.max(ans, i-start-1); start = i; } } return Math.max(ans, a.length-start-1); } }
golang 解法, 执行用时: 8 ms, 内存消耗: 3.5 MB, 提交时间: 2023-09-13 15:53:41
func temperatureTrend(a, b []int) (ans int) { start := 0 for i := 1; i < len(a); i++ { if a[i] == a[i-1] != (b[i] == b[i-1]) || a[i] < a[i-1] != (b[i] < b[i-1]) { ans = max(ans, i-start-1) start = i } } return max(ans, len(a)-start-1) } func max(a, b int) int { if b > a { return b }; return a }
python3 解法, 执行用时: 44 ms, 内存消耗: 15.1 MB, 提交时间: 2022-10-09 10:54:30
class Solution: def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int: diff1 = [temperatureA[i] - temperatureA[i-1] for i in range(1,len(temperatureA))] diff2 = [temperatureB[i] - temperatureB[i-1] for i in range(1,len(temperatureB))] res = cur = 0 for i,j in zip(diff1,diff2): if i * j > 0 or i == j == 0: cur += 1 else: cur = 0 res = max(res, cur) return res
python3 解法, 执行用时: 40 ms, 内存消耗: 15 MB, 提交时间: 2022-10-09 10:51:54
class Solution: def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int: ans = cnt = 0 for (a1, b1), (a2, b2) in pairwise(zip(temperatureA, temperatureB)): if (a1 > a2) - (a1 < a2) == (b1 > b2) - (b1 < b2): cnt += 1 ans = max(ans, cnt) else: cnt = 0 return ans