Skip to content

Latest commit

 

History

History
21 lines (17 loc) · 525 Bytes

2610. Convert an Array Into a 2D Array With Conditions.md

File metadata and controls

21 lines (17 loc) · 525 Bytes

Code for "2610. Convert an Array Into a 2D Array With Conditions" (JAVA)

class Solution {
  public List<List<Integer>> findMatrix(int[] nums) {
    // The number of rows we need equals the maximum frequency.
    List<List<Integer>> ans = new ArrayList<>();
    int[] count = new int[nums.length + 1];

    for (final int num : nums) {
      // Construct `ans` on demand.
      if (++count[num] > ans.size())
        ans.add(new ArrayList<>());
      ans.get(count[num] - 1).add(num);
    }

    return ans;
  }
}