Header Ad

HackerEarth Roy's Life Cycle problem solution

In this HackerEarth Roy's Life Cycle problem solution 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 a longest coding streak of the day - X.
He also wants to find the longest coding streak of N days - Y.
Coding streak means a number of C's without any E or S in between.


HackerEarth Roy's Life Cycle problem solution


HackerEarth Roy's Life Cycle problem solution.

def find_longest_streak(s,slen):
longest_streak = 0
streak = 0
for i in xrange(slen):
if s[i] == 'C':
streak += 1
else:
streak = 0
longest_streak = max(streak,longest_streak)
return longest_streak

def roys_life_cycle():
month_life = ''
N = input()
day_streak = []
for i in xrange(N):
day = raw_input()
dlen = len(day)
day_streak.append(find_longest_streak(day,dlen))
month_life += day
print max(day_streak),find_longest_streak(month_life,len(month_life))
roys_life_cycle()


Second solution

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define LL long long
#define ULL unsigned long long
#define F first
#define S second
#define pb push_back
#define FOR(i,lb,ub) for(i=lb;i<=ub;i++)
#define RFOR(i,ub,lb) for(i=ub;i>=lb;i--)
#define FORS(it,v) for(it=v.begin();it!=v.end();it++)
LL gcd(LL a, LL b) { return b?gcd(b,a%b):a; }
using namespace std;
string s[365];
int find_streak_X(int i)
{
int res=0,j;
for (j=0; j<s[i].size();)
{
int temp=0;
while (j<s[i].size() && s[i][j]=='C') temp++,j++;
res = max(res,temp);
while (j<s[i].size() && s[i][j]!='C') j++;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
int i,j,n;
cin>>n;
assert(n>=1 && n<=365);
FOR(i,0,n-1) cin>>s[i];

FOR(i,0,n-1)
{
int m = s[i].size();
assert(m==1440);
FOR(j,0,m-1)
assert(s[i][j]=='S' || s[i][j]=='E' || s[i][j]=='C');
}

int X=0,Y=0;
FOR(i,0,n-1)
{
X = max(X,find_streak_X(i));
}
int temp=0;
FOR(i,0,n-1)
{
int m = s[i].size();
FOR(j,0,m-1)
{
if (s[i][j]=='C') temp++;
else
{
if (temp>Y) Y = temp;
temp=0;
}
}

}
if (temp>Y) Y = temp;
cout<<X<<" "<<Y<<endl;
return 0;
}

Post a Comment

0 Comments