Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package g2601_2700.s2610_convert_an_array_into_a_2d_array_with_conditions;

// #Medium #Array #Hash_Table #2023_08_30_Time_2_ms_(97.24%)_Space_43.9_MB_(80.58%)
// #Medium #Array #Hash_Table #2025_02_25_Time_1_ms_(100.00%)_Space_45.01_MB_(53.07%)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
public List<List<Integer>> findMatrix(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for (int x : nums) {
map.put(x, map.getOrDefault(x, 0) + 1);
int idx = map.get(x);
if (res.size() < idx) {
int n = nums.length;
int[] freq = new int[n + 1];
for (int num : nums) {
if (res.size() < ++freq[num]) {
res.add(new ArrayList<>());
}
res.get(idx - 1).add(x);
res.get(freq[num] - 1).add(num);
}
return res;
}
Expand Down