There is x number of girls and they rolled dice in turns one after another.
If the number on the dice is 6, then the dice will be rolled again until she gets a number other than 6.
Since you know the sequence of numbers which the dice shows when rolled each time, you have to find what is the total number of girls or if the sequence is invalid.
Example:
Input: s = "3662123"
Output: 5
Approach
C++
#include <bits/stdc++.h>using namespace std;int theDice(string s){int n = s.size();int i = 0, cnt = 0;while (i < n){while (i < n && s[i] == '6')i++;cnt++;i++;}if (s[n - 1] == '6')return -1;elsereturn cnt;}int main(){string s = "3662123";cout << theDice(s) << "\n";return 0;}
No comments:
Post a Comment