Problem solution · C++

Distribute Elements Into Two Arrays II

Distribute Elements Into Two Arrays II: a C++ solution using sorting and greedy selection. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sorting and greedy selection
Source
walkccc LeetCode Solutions
Length
74 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sorting and greedy selection

For Distribute Elements Into Two Arrays II, the implementation first exposes a useful order, then scans that order while making locally justified choices.

  1. Choose the key that reveals the greedy or grouping structure.
  2. Sort the relevant records by that key.
  3. Scan in order, maintaining the invariant that makes each local choice safe.

Code notes

  • 74 lines of C++ from the credited upstream file 3072.cpp.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 4 loop blocks detected.

Complexity

Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeDistribute Elements Into Two Arrays II · C++C++
Use this to learn the idea, then write your own version.
class FenwickTree { public:  FenwickTree(int n) : sums(n + 1) {}   void add(int i, int delta) {    while (i < sums.size()) {      sums[i] += delta;      i += lowbit(i);    }  }   int get(int i) const {    int sum = 0;    while (i > 0) {      sum += sums[i];      i -= lowbit(i);    }    return sum;  }  private:  vector<int> sums;   static inline int lowbit(int i) {    return i & -i;  }}; class Solution { public:  vector<int> resultArray(vector<int>& nums) {    vector<int> arr1;    vector<int> arr2;    const unordered_map<int, int> ranks = getRanks(nums);    FenwickTree tree1(ranks.size());    FenwickTree tree2(ranks.size());     add(nums[0], arr1, tree1, ranks);    add(nums[1], arr2, tree2, ranks);     for (int i = 2; i < nums.size(); ++i) {      const int greaterCount1 = arr1.size() - tree1.get(ranks.at(nums[i]));      const int greaterCount2 = arr2.size() - tree2.get(ranks.at(nums[i]));      if (greaterCount1 > greaterCount2)        add(nums[i], arr1, tree1, ranks);      else if (greaterCount1 < greaterCount2)        add(nums[i], arr2, tree2, ranks);      else if (arr1.size() > arr2.size())        add(nums[i], arr2, tree2, ranks);      else        add(nums[i], arr1, tree1, ranks);    }     arr1.insert(arr1.end(), arr2.begin(), arr2.end());    return arr1;  }  private:  unordered_map<int, int> getRanks(const vector<int>& nums) {    unordered_map<int, int> ranks;    set<int> sorted(nums.begin(), nums.end());    int rank = 0;    for (const int num : sorted)      ranks[num] = ++rank;    return ranks;  }   void add(int num, vector<int>& arr, FenwickTree& tree,           const unordered_map<int, int>& ranks) {    arr.push_back(num);    tree.add(ranks.at(num), 1);  };}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗