Problem solution · Java

Distribute Repeating Integers

Distribute Repeating Integers: a Java solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Dynamic programming
Source
walkccc LeetCode Solutions
Length
54 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

Dynamic programming

For Distribute Repeating Integers, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 54 lines of Java from the credited upstream file 1655.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup, cached states.
  • 7 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

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 Repeating Integers · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  public boolean canDistribute(int[] nums, int[] quantity) {    List<Integer> freqs = getFreqs(nums);    // validDistribution[i][j] := true if it's possible to distribute the i-th    // freq into a subset of quantity represented by the bitmask j    boolean[][] validDistribution = getValidDistribution(freqs, quantity);    final int n = freqs.size();    final int m = quantity.length;    final int maxMask = 1 << m;    // dp[i][j] := true if it's possible to distribute freqs[i..n), where j is    // the bitmask of the selected quantity    boolean[][] dp = new boolean[n + 1][maxMask];    dp[n][maxMask - 1] = true;     for (int i = n - 1; i >= 0; --i)      for (int mask = 0; mask < maxMask; ++mask) {        dp[i][mask] = dp[i + 1][mask];        final int availableMask = ~mask & (maxMask - 1);        for (int submask = availableMask; submask > 0; submask = (submask - 1) & availableMask)          if (validDistribution[i][submask])            dp[i][mask] = dp[i][mask] || dp[i + 1][mask | submask];      }     return dp[0][0];  }   private List<Integer> getFreqs(int[] nums) {    List<Integer> freqs = new ArrayList<>();    Map<Integer, Integer> count = new HashMap<>();    for (final int num : nums)      count.merge(num, 1, Integer::sum);    return new ArrayList<>(count.values());  }   boolean[][] getValidDistribution(List<Integer> freqs, int[] quantity) {    final int maxMask = 1 << quantity.length;    boolean[][] validDistribution = new boolean[freqs.size()][maxMask];    for (int i = 0; i < freqs.size(); ++i)      for (int mask = 0; mask < maxMask; ++mask)        if (freqs.get(i) >= getQuantitySum(quantity, mask))          validDistribution[i][mask] = true;    return validDistribution;  }   // Returns the sum of the selected quantity represented by `mask`.  int getQuantitySum(int[] quantity, int mask) {    int sum = 0;    for (int i = 0; i < quantity.length; ++i)      if ((mask >> i & 1) == 1)        sum += quantity[i];    return sum;  }} 

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 ↗