Approach
Sorting and greedy selection
For Count Number of Balanced Permutations, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 94 lines of C++ from the credited upstream file 3343.cpp.
- The implementation visibly relies on sequence storage.
- 6 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.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 int countBalancedPermutations(string num) {4 vector<int> nums = getNums(num);5 const int sum = accumulate(nums.begin(), nums.end(), 0);6 if (sum % 2 == 1)7 return 0;8 9 ranges::sort(nums, greater<>());10 11 const int even = (nums.size() + 1) / 2;12 const int odd = nums.size() / 2;13 const int evenBalance = sum / 2;14 vector<vector<vector<long>>> mem(15 even + 1,16 vector<vector<long>>(odd + 1, vector<long>(evenBalance + 1, -1)));17 const long perm = getPerm(nums);18 return countBalancedPermutations(nums, even, odd, evenBalance, mem) *19 modInverse(perm) % kMod;20 }21 22 private:23 static constexpr int kMod = 1'000'000'007;24 25 26 27 28 long countBalancedPermutations(const vector<int>& nums, int even, int odd,29 int evenBalance,30 vector<vector<vector<long>>>& mem) {31 if (evenBalance < 0)32 return 0;33 if (even == 0)34 return evenBalance == 0 ? factorial(odd) : 0;35 const int index = nums.size() - (even + odd);36 if (odd == 0) {37 long remainingSum = 0;38 for (int i = index; i < nums.size(); ++i)39 remainingSum += nums[i];40 return (remainingSum == evenBalance) ? factorial(even) : 0;41 }42 if (mem[even][odd][evenBalance] != -1)43 return mem[even][odd][evenBalance];44 const long placeEven =45 countBalancedPermutations(nums, even - 1, odd,46 evenBalance - nums[index], mem) *47 even % kMod;48 const long placeOdd =49 countBalancedPermutations(nums, even, odd - 1, evenBalance, mem) * odd %50 kMod;51 return mem[even][odd][evenBalance] = (placeEven + placeOdd) % kMod;52 }53 54 vector<int> getNums(const string& num) {55 vector<int> nums;56 for (const char c : num)57 nums.push_back(c - '0');58 return nums;59 }60 61 long getPerm(const vector<int>& nums) {62 long res = 1;63 vector<int> count(10);64 for (const int num : nums)65 ++count[num];66 for (const int freq : count)67 res = res * factorial(freq) % kMod;68 return res;69 }70 71 long factorial(int n) {72 long res = 1;73 for (int i = 2; i <= n; ++i)74 res = res * i % kMod;75 return res;76 }77 78 long modInverse(long a) {79 long m = kMod;80 long y = 0;81 long x = 1;82 while (a > 1) {83 const long q = a / m;84 long t = m;85 m = a % m;86 a = t;87 t = y;88 y = x - q * y;89 x = t;90 }91 return x < 0 ? x + kMod : x;92 }93};94