- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to count minimum how many minutes after there will be no new angry students
Suppose we have a string S of length n, with only two types of characters, 'A' or 'P'. There are n students in a row, the ith student is angry if S[i] = 'A', if it is 'P' it says S[i] is patient. An angry student at index i will hit patient student in index i+1 in every minute, and for the last student even it he is angry, he cannot hit anyone. After hitting a patient student, that student also gets angry. We have to find the minimum minutes for after that no new students get angry.
So, if the input is like S = "PPAPP", then the output will be 2, because after first minute, the string will be "PPAAP", then in second minute "PPAAA", then no new student gets angry again.
Steps
To solve this, we will follow these steps −
n := size of S ans := 0, cnt = 0 for initialize i := n - 1, when i >= 0, update (decrease i by 1), do: if S[i] is same as 'P', then: (increase cnt by 1) Otherwise ans := maximum of ans and cnt cnt := 0 return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(string S) { int n = S.size(); int ans = 0, cnt = 0; for (int i = n - 1; i >= 0; i--) { if (S[i] == 'P') { cnt++; } else { ans = max(ans, cnt); cnt = 0; } } return ans; } int main() { string S = "PPAPP"; cout << solve(S) << endl; }
Input
PPAPP
Output
2
- Related Articles
- In a computer lab, there are $3$ computers for every $6$ students. How many computers will be needed for $24$ students?
- C++ program to choose some numbers for which there will be no subset whose sum is k
- C++ program to count how many minutes we have to wait to meet at least one swimmer
- C++ Program to count minimum problems to be solved to make teams
- What will happen if there will be no cone cells in our retina?
- Program to count how many swimmers will win the final match in Python
- Program to count minimum number of animals which have no predator in Python
- C++ program to check how many students have greater score than first one
- Program to count number of switches that will be on after flipping by n persons in python
- Program to count number of palindromes after minimum number of split of the string in C++
- A school collected Rs. 2304 as fees from its students. If each student paid as many paise as there were students in the school, how many students were there in the school?
- C++ program to find minimum how many operations needed to make number 0
- C++ program to find minimum how many coins needed to buy binary string
- Program to count how many numbers should be appended to create all numbers from 1 to k in C++
- C++ program to count final number of characters are there after typing n characters in a crazy writer
