Skip to content

Commit

Permalink
June21: Trend of temperature change, lambda function [E]
Browse files Browse the repository at this point in the history
auto cmp = [](int x, int y) { return (x > y) - (x < y); };

This is a basic lambda function.
  • Loading branch information
aucker committed Jun 21, 2024
1 parent dec54e2 commit 66eed04
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions daily/June21.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief LCP: 61: trend of temperature change [E]
* Time: O(N), Space: O(1)
*
* @param temperatureA
* @param temperatureB
* @return int
*/
int temperatureTrend(vector<int>& temperatureA, vector<int>& temperatureB) {
auto cmp = [](int x, int y) { return (x > y) - (x < y); };

int ans = 0, same = 0;
for (int i = 1; i < temperatureA.size(); i++) {
if (cmp(temperatureA[i - 1], temperatureA[i]) ==
cmp(temperatureB[i - 1], temperatureB[i])) {
ans = max(ans, ++same);
} else {
same = 0;
}
}
return ans;
}
};

0 comments on commit 66eed04

Please sign in to comment.