Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2024-06-06 13:27:02
※ 引述 《SecondRun (南爹摳打)》 之銘言:
:  
: 846. Hand of Straights
:  
: 給定一整數陣列和分組大小
: 回傳可否把數字分組,每一組的數字要連續
:  
: Example 1:
: Input: hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
: Output: true
: Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
:  
: Example 2:
: Input: hand = [1,2,3,4,5], groupSize = 4
: Output: false
: Explanation: Alice's hand can not be rearranged into groups of 4.
:
思路 :
反正一定是要每一個數字都用來排他的卡組
先看能不能整除
然後就把每個數字都塞進map
因為要有序
所以就每一次都拿最小的數字組成卡組
然後刪除他們
如果可以組成的話就 可以
不行就return false
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize)
{
int len = hand.size();
if(len%groupSize != 0)return false;
map<int,int> paper;
for(int k : hand)
{
paper[k]++;
}
while(!paper.empty())
{
vector<int> now;
for(auto it = paper.begin() ; it != paper.end() ; it ++)
{
now.push_back(it->first);
it->second
作者: Smallsh (Smallsh)   2023-06-06 13:27:00
大師
作者: orangeNoob (橘子色的肥肥)   2024-06-06 13:30:00
別捲了

Links booklink

Contact Us: admin [ a t ] ucptt.com