<p>Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size <code>groupSize</code>, and consists of <code>groupSize</code> consecutive cards.</p>

<p>Given an integer array <code>hand</code> where <code>hand[i]</code> is the value written on the <code>i<sup>th</sup></code> card and an integer <code>groupSize</code>, return <code>true</code> if she can rearrange the cards, or <code>false</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
<strong>Output:</strong> true
<strong>Explanation:</strong> Alice&#39;s hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> hand = [1,2,3,4,5], groupSize = 4
<strong>Output:</strong> false
<strong>Explanation:</strong> Alice&#39;s hand can not be rearranged into groups of 4.

</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= hand.length &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= hand[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= groupSize &lt;= hand.length</code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as 1296: <a href="https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/" target="_blank">https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/</a></p>

// class Solution {
// public:
//     bool isNStraightHand(vector<int>& hand, int groupSize) {
//         if (hand.size() % groupSize != 0) { return false; }
//         std::sort(hand.begin(), hand.end());
//         int hand_size = 0;
//         int prev_unique;
//         for (int idx = 0; idx < hand.size(); ++idx) {
//             if (hand.at(idx) < 0) { continue; }
//             if (hand_size == 0) {
//                 prev_unique = hand.at(idx);
//                 ++hand_size;
//             } else {
//                 int next_unique_idx = idx;
//                 while (hand.at(next_unique_idx) == prev_unique || hand.at(next_unique_idx) < 0) {
//                     ++next_unique_idx;
//                     if (next_unique_hand == hand.size()) { return false; }
//                 }
//                 if (hand.at(idx) - prev_unique != 1) { return false; }
//                 ++hand_size;
//             }
//         }
//     }
// };

class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int groupSize) {
        if (hand.size() % groupSize != 0) { return false; }
        const int n = hand.size();
        std::sort(hand.begin(), hand.end());
        std::vector<vector<int>> hand_bucket;
        // int[n / groupSize][groupSize] hand_bucket;
        // for (int i = 0; i < n / groupSize; ++i) {
        //     for (int j = 0; j < groupSize; ++j) {
        //         hand_bucket = -1;
        //     }
        // }
        for (const int& value : hand) {
            bool taken = false;
            for (vector<int>& hands : hand_bucket) {
                if (hands.size() < groupSize && value - hands.back() == 1) {
                    taken = true;
                    hands.push_back(value);
                    break;
                }
            }
            if (taken) { continue; }
            if (hand_bucket.size() < n / groupSize) {
                hand_bucket.push_back({value});
                continue;
            }
            // for (const vector<int>& hands : hand_bucket) {
            //     for (const int v : hands) {
            //         std::cout << v << " ";
            //     }
            //     std::cout << "\n";
            // }
            // std::cout << value;
            return false;
        }
        // for (const vector<int>& hands : hand_bucket) {
        //     if (hands.size() != )
        // }
        return true;
    }
};

27315