LC 氣溫變化趨勢

vil4發表於2024-06-24

簡單的開篇。

#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
	int temperatureTrend(vector<int>& temperatureA, vector<int>& temperatureB) {
		int length = temperatureA.size();
		int maxTend = 0, tendNow = 0;
		auto GetTodayTend = [&](int gap) {
			if (gap == 0)return 0;
			return gap > 0 ? 1 : -1;
		};
		for (int i = 1; i < length; i++)
		{
			bool isDaySame = GetTodayTend(temperatureA[i] - temperatureA[i - 1]) == GetTodayTend(temperatureB[i] - temperatureB[i - 1]);
			if (isDaySame) {
				++tendNow;
				maxTend = max(maxTend, tendNow);
			}
			else {
				tendNow = 0;
			}
		}
		return maxTend;
	}
};

相關文章