Skip to content

Commit 0428285

Browse files
committed
Runtime: 61 ms (Top 63.76%) | Memory: 95 MB (Top 59.07%)
1 parent 9f6b0bd commit 0428285

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
// Runtime: 61 ms (Top 63.76%) | Memory: 95 MB (Top 59.07%)
12
class Solution {
23
public int totalFruit(int[] fruits) {
34
if (fruits == null || fruits.length == 0) {
45
return 0;
56
}
67
int start = 0, end = 0, res = 0;
78
HashMap<Integer, Integer> map = new HashMap<>(); //key = type of fruit on tree, value = last index / newest index of that fruit
8-
9+
910
while (end < fruits.length) {
1011
if (map.size() <= 2) {
1112
map.put(fruits[end], end);
1213
end++;
1314
}
14-
15+
1516
if (map.size() > 2) {
1617
int leftMost = fruits.length;
1718
for (int num : map.values()) {
@@ -20,9 +21,9 @@ public int totalFruit(int[] fruits) {
2021
map.remove(fruits[leftMost]);
2122
start = leftMost + 1;
2223
}
23-
24+
2425
res = Math.max(res, end - start);
2526
}
2627
return res;
2728
}
28-
}
29+
}

0 commit comments

Comments
 (0)