File tree Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -4,20 +4,22 @@ public int totalFruit(int[] tree) {
4
4
return 0 ;
5
5
}
6
6
7
- int start = 0 , end = 0 , result = 0 ;
8
7
Map <Integer , Integer > hm = new HashMap <>();
8
+ int s = 0 , e = 0 , result = 0 ;
9
9
10
- while (end < tree .length ) {
11
- hm .put (tree [end ], end ++);
10
+ while (e < tree .length ) {
11
+ hm .put (tree [e ], hm .getOrDefault (tree [e ], 0 ) + 1 );
12
+ ++e ;
12
13
13
- if (hm .size () > 2 ) {
14
- int minIdx = Collections .min (hm .values ());
15
- start = minIdx + 1 ;
16
-
17
- hm .remove (tree [minIdx ]);
14
+ while (hm .size () > 2 ) {
15
+ hm .put (tree [s ], hm .get (tree [s ]) - 1 );
16
+ if (hm .get (tree [s ]) == 0 ) {
17
+ hm .remove (tree [s ]);
18
+ }
19
+ ++s ;
18
20
}
19
21
20
- result = Math .max (result , end - start );
22
+ result = Math .max (result , e - s );
21
23
}
22
24
23
25
return result ;
You can’t perform that action at this time.
0 commit comments