File tree Expand file tree Collapse file tree 1 file changed +6
-9
lines changed
src/main/java/g2601_2700/s2610_convert_an_array_into_a_2d_array_with_conditions Expand file tree Collapse file tree 1 file changed +6
-9
lines changed Original file line number Diff line number Diff line change 11package g2601_2700 .s2610_convert_an_array_into_a_2d_array_with_conditions ;
22
3- // #Medium #Array #Hash_Table #2023_08_30_Time_2_ms_(97.24%)_Space_43.9_MB_(80.58 %)
3+ // #Medium #Array #Hash_Table #2025_02_25_Time_1_ms_(100.00%)_Space_45.01_MB_(53.07 %)
44
55import java .util .ArrayList ;
6- import java .util .HashMap ;
76import java .util .List ;
8- import java .util .Map ;
97
108public class Solution {
119 public List <List <Integer >> findMatrix (int [] nums ) {
1210 List <List <Integer >> res = new ArrayList <>();
13- Map <Integer , Integer > map = new HashMap <>();
14- for (int x : nums ) {
15- map .put (x , map .getOrDefault (x , 0 ) + 1 );
16- int idx = map .get (x );
17- if (res .size () < idx ) {
11+ int n = nums .length ;
12+ int [] freq = new int [n + 1 ];
13+ for (int num : nums ) {
14+ if (res .size () < ++freq [num ]) {
1815 res .add (new ArrayList <>());
1916 }
20- res .get (idx - 1 ).add (x );
17+ res .get (freq [ num ] - 1 ).add (num );
2118 }
2219 return res ;
2320 }
You can’t perform that action at this time.
0 commit comments