讨论/《美团 2021 届秋季校园招聘笔试真题》 - 偏爱字母/
《美团 2021 届秋季校园招聘笔试真题》 - 偏爱字母
共 2 个回复
#include <iostream>

using namespace std;

int n;
string s;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> s;

    int res = 0, sum = 0;
    for (int i = 0; i < n; i++) {
        int x = s[i] == 'E' ? 1 : -1;
        sum = x + max(sum, 0);
        res = max(res, sum);
    }
    cout << res << endl;

    return 0;
}
import java.io.*;
import java.util.*;

class Solution {
    public static void main(String[] args) throws IOException {
        var reader = new BufferedReader(new InputStreamReader(System.in));
        var writer = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(reader.readLine());
        char[] s = reader.readLine().toCharArray();

        int res = 0, sum = 0;
        for (char c : s) {
            int x = c == 'E' ? 1 : -1;
            sum = x + Math.max(sum, 0);
            res = Math.max(res, sum);
        }
        writer.write(res + "\n");
        
        writer.flush();
        reader.close();
        writer.close();
    }
}
1
import java.util.*;

class Solution{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String s = sc.next();

        int left = -1, right = 0, res = 0, count = 0;
        while (right < s.length()){
            if (s.charAt(right) == 'E'){
                count++;
                res = Math.max(res, count);
                right++;
            }else{
                count = count - 1 < 0 ? 0 : count - 1;
                if (count == 0){
                    left = right;
                }
                right++;
            }
        }

        System.out.println(res);

    }
}