气温变化趋势

标签: 数组

难度: Easy

力扣城计划在两地设立「力扣嘉年华」的分会场,气象小组正在分析两地区的气温变化趋势,对于第 `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` ![image.png](https://pic.leetcode-cn.com/1663902654-hlrSvs-image.png){: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`

Submission

运行时间: 30 ms

内存: 16.3 MB

class Solution:
    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
        t1,t2,l = [],[],len(temperatureA)
        for i in range(1,l):
            n1,n2 = temperatureA[i]-temperatureA[i-1],temperatureB[i]-temperatureB[i-1]
            t1.append(n1//abs(n1) if n1 != 0 else 0)
            t2.append(n2//abs(n2) if n2 != 0 else 0)
        
        ans,days = 0,0
        for i in range(l-1):
            if t1[i] != t2[i]:
                ans = max(ans,days)
                days = 0
            else:
                days += 1
        ans = max(ans,days)
        return ans

Explain

The provided solution aims to find the longest contiguous subarray where the temperature trends between two locations are the same. Firstly, it calculates the daily temperature change for each location and represents it as -1, 0, or 1 (decrease, no change, increase). These trends are stored in two lists (t1 for location A, t2 for location B). Then, by iterating over these trend lists, the solution counts the length of contiguous subarrays where the trends match. The maximum length found during this process is the output.

时间复杂度: O(n)

空间复杂度: O(n)

class Solution:
    def temperatureTrend(self, temperatureA: List[int], temperatureB: List[int]) -> int:
        t1, t2, l = [], [], len(temperatureA)
        # Compute trend differences for each day for both locations
        for i in range(1, l):
            n1, n2 = temperatureA[i] - temperatureA[i-1], temperatureB[i] - temperatureB[i-1]
            # Convert differences to -1, 0, 1 representing down, same, up
            t1.append(n1 // abs(n1) if n1 != 0 else 0)
            t2.append(n2 // abs(n2) if n2 != 0 else 0)
        
        ans, days = 0, 0
        # Compare trend arrays to find the longest matching subarray
        for i in range(l-1):
            if t1[i] != t2[i]:
                ans = max(ans, days)  # Update maximum length found
                days = 0  # Reset days counter when mismatch found
            else:
                days += 1  # Increment days counter when trends match
        ans = max(ans, days)  # Check last subarray
        return ans

Explore

在计算温度趋势时,使用-1、0、1来表示温度变化(下降、不变、上升)主要是为了简化问题并减少不必要的复杂性。直接使用温度差会包含更多的细节,比如具体温度差的大小,但这些细节对于判断温度的趋势(是否上升或下降)并不是必需的。使用简化的表示法可以更容易地比较两地的温度趋势是否一致,从而快速找到最长匹配的子数组。

将温度差为0时的趋势值设置为0是合理的,因为它正确地反映了温度没有变化的情况。这种处理方式不会影响结果的准确性,因为题目的目标是找到两地温度趋势完全相同的最长时间段。如果连续多天温度不变,这应当被视为一种特定的趋势(即不变),并且如果两地在这些天都没有温度变化,这些天应该被计入最长匹配的子数组中。

在代码中,当发现两地的温度趋势不匹配时,需要将天数计数器`days`重置为0,因为我们正在寻找最长的连续匹配的子数组。一旦趋势不匹配,之前的连续匹配就被中断了,我们需要重新开始计数以寻找下一个可能的匹配子数组。至于避免频繁重置操作,实际上重置操作是这种问题求解过程中的必要步骤,因为它标志着新的匹配子数组的开始。没有其他方法可以避免这个重置步骤,因为每次趋势不匹配时,都意味着之前的连续匹配已经结束。