Roy is going through the dark times of his life. Recently his girlfriend broke up with him and to overcome the pain of acute misery he decided to restrict himself to the Eat-Sleep-Code life cycle. For N days he did nothing but eat, sleeps and code.
A close friend of Roy kept an eye on him for the last N days. For every single minute of the day, he kept track of Roy's actions and prepared a log file.
The log file contains exactly N lines, each line contains a string of length 1440 ( i.e. number of minutes in 24 hours of the day).
The string is made of characters E, S, and C only; representing Eat, Sleep and Code respectively. the ith character of the string represents what Roy was doing during an ith minute of the day.
Roy's friend is now interested in finding out the maximum of the longest coding streak of the day - X.
He also wants to find the longest coding streak of N days - Y.
Coding streak means the number of C's without any E or S in between.
Example:
Input: n = 4, s = {"SSSSEEEECCCCEECCCC", "CCCCCSSSSEEECCCCSS", "SSSSSEEESSCCCCCCCS", "EESSSSCCCCCCSSEEEE"}
Output: 7 9
Approach
C++
#include <bits/stdc++.h>using namespace std;void longestCodingStreak(long long n, vector<string> &s){long long res = 0;long long cnt = 0;for (long long i = 0; i < n; i++){string str = s[i];cnt = 0;for (long long j = 0; j < str.size(); j++){if (str[j] == 'C')cnt++;else{res = max(res, cnt);cnt = 0;}}res = max(res, cnt);}res = max(res, cnt);long long res1 = 0;long long cnt1 = 0;for (long long i = 0; i < n; i++){string str = s[i];for (long long j = 0; j < str.size(); j++){if (str[j] == 'C')cnt1++;else{res1 = max(res1, cnt1);cnt1 = 0;}}}res1 = max(res1, cnt1);cout << res << " " << res1 << "\n";}int main(){long long n = 4;vector<string> s = {"SSSSEEEECCCCEECCCC","CCCCCSSSSEEECCCCSS","SSSSSEEESSCCCCCCCS","EESSSSCCCCCCSSEEEE"};longestCodingStreak(n, s);return 0;}
No comments:
Post a Comment