Professional Documents
Culture Documents
Biweekly Contest 83
Biweekly Contest 83
The following are the types of poker hands you can make from best to worst:
Return a string representing the best type of poker hand you can make with the given
cards.
Example 1:
Output: "Flush"
Explanation: The hand with all the cards consists of 5 cards with the
same suit, so we have a "Flush".
Approach:
Code:
string bestHand(vector<int>& ranks, vector<char>& suits) {
unordered_map<int,int>r;
unordered_map<char,int>s;
for(int i=0;i<ranks.size();i++)
{
r[ranks[i]]++;
}for(int i=0;i<suits.size();i++)
{
s[suits[i]]++;
}
for(auto it: s)
{
if(it.second==5)
return "Flush";
}
for(auto it:r)
{
if(it.second>=3)
return "Three of a Kind";
}
for(auto it:r)
{
if(it.second==2)
return "Pair";
}
return "High Card";
}
2. Medium Level: Number of Zero-Filled Subarrays.
Code:
Input: nums = [1,3,0,0,2,0,0,4]
Output: 6
Explanation:
Approach:
After reading problem statement, we return count of zero
subarray.
So for that I iterate in vector and check if nums[i]==0 then
increment cnt value, and then in else condition I make an equation
For counting subarray that is total+=(c*(c+1)/2)
Like in {1,3,0,0,2,0,0,4}
At index 2 and 3 we have zero so cnt=2 then total = 0+(6/2)
Total=3; and do cnt=0;
Then again got zero at 5 and 6 index again tot=3
Then total is 3+3 =6
So return 6;
Code:
long long zeroFilledSubarray(vector<int>& nums) {