题解 | #游游的最长稳定子数组#

游游的最长稳定子数组

https://www.nowcoder.com/practice/ea7098b7960348f6915e252f0c4debcc

o(n)遍历一遍即可。

#include<bits/stdc++.h>
#define int long long
#define double long double
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
const int M = 1e3 + 10;
int mod = 1e9 + 7;
int a[N];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int maxx = 0, cnt = 0;
    a[n + 1] = 1e18;
    for (int i = 1; i <= n; i++) {
        if (abs(a[i] - a[i + 1]) > 1) {
            maxx = max(maxx, cnt + 1);
            cnt = 0;
        } else cnt++;
    }
    cout << maxx << "\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _;
    _ = 1;
    //cin>>_;
    while (_--) {
        solve();
    }
}

全部评论

相关推荐

hwwhwh:同双非,有大厂实习其实也没啥用,主要看运气,等就行了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务