Hands of Straights Coding Solution Geeks for Geeks Problem of the Day 20 May 2023
Input:
GFG POTD Solution Today
Gfg problem of the day solution with explanation
C++
class Solution {
public:
bool isStraightHand(int n, int k, vector &v) {
// k= group size
if(n%k>0){
return false;
}
map mp;
for(auto it:v){
mp[it]++;
}
// put all the size() with its frquency in the (min)p_queue
priority_queue , vector>, greater> > pq;
for(auto it:mp){
pq.push({it.first,it.second});
}
int cnt=0;
int last=-1;
queue> q;
while(pq.size()>0){
int t=pq.top().first;
int val=pq.top().second;
pq.pop();
// take top ele of the heap and check if the last element and
// the top element are coincidnt if not return false
// if yes then put the last ele to curr op element and move forward
if(val-1>0){
q.push({t,val-1});
}
// unit the group size is not reach dont re push the value of heap by subtracting one from it
// to pq again till then put the value in queue
cnt++;
if(last>=0){
if(t-last!=1){
return false;
}else{
last=t;
}
}
last=t;
if(cnt==k){
while(q.size()>0){
pq.push(q.front());
q.pop();
}
last=-1;
cnt=0;
}
}
// if int the end it finds that the value of cnt is not 0 means still some element left int group
if(cnt>0){
return false;
}
return true;
}
};
N = 9
groupSize = 3
hand[ ] = {1, 2, 3, 6, 2, 3, 4, 7, 8}
Output: true
Input:
N = 5
groupSize = 2
hand[ ] = {1, 2, 3, 4, 5}
Output: false
GFG POTD Answer in C++
Gfg problem of the day in python
Gfg problem of the day in java
Comments
Post a Comment